ISYS 303 - Ch. 5-7, ArrayList Flashcards
Null Array
In order to declare an array that you want to resize later, then you need to assign it a null value:
String[] asNames = null;
Array
Collection of variables of the same type, referred to by a common name.
An array is a set of contiguous memory locations, Once you declare the array you cannot change the size, An array is an object and it has attributes
Declare Array (One-Dimensional)
When you declare the array you have to use the “new” keyword and give it a size (unless you plan on resizing it later - null array)
type array-name[] = new type[size]
- **type declares the element type of the array, and determines the data type of each element contained in the array
- **the number of elements that the array will hold is determined by size
Creation of an Array
Since arrays are implemented as objects, the creation of an array is a two-step process. First you declare an array reference variable. Second, you allocate memory for the array, assigning a reference to that memory to the array variable.
int sample[] = new int[10]
***sample holds a reference to the memory allocated by new
int sample[]; //no physical object
sample = new int[10]; //now created an object and linked to an array
The [] can go on the data type or on the variable name:
String[] asNames = new String[3];
IS THE SAME AS
String asNames[] = new String[3];
Find MIN/MAX
min = max = sample[0] for(iCount = 1; iCount max) max = sample[iCount];
index
An individual element within an array is accessed by use of an index. An index describes the position of an element within an array. All arrays have zero as the index of their first element.
You access each element of the array by using the index.
The index is 0 based meaning that if you create an array that is 3 long, the locations or indexes are 0, 1, and 2
asNames[0] = "Greg"; asNames[1] = "Joe";
.length
One attribute is length and it tells you the size of the array or the number of elements in the array
Initialized arrays
Arrays can be initialized when they are created. The general form for initializing arrays is:
type array-name[] = {value1, value2, value 3,…, valueN}
Sorting an Array
Bubble sort uses the repeated comparison and, if necessary, exchange of adjacent elements in the array. Small values move toward one end and vice versa. Operates by making several passes through the array, exchanging out of place elements when necessary. The number of passes required to ensure the array is sorted is equal to one less than the number of elements in the array.
Bubble Sort Code
//this is the bubble sort for(a = 1; a = a; b--) { if(nums[b-1] > nums[b]) { t = nums[b-1]; nums[b-1] = nums[b]; nums[b] = t; } } }
Strings in Arrays
- Strings are really just an array of characters
- They also have attributes and methods
String Array Methods
-One of them is length which tells you how many characters is in the string:
asNames[1].length
-Another method is equals which does a comparison to another string:
if (asNames[1].equals(“Greg”))
-Another method is equalsIgnoreCase which does a comparison to another string but ignores the case:
if (asNames[1].equalsIgnoreCase(“joe”))
Two/Multi-Dimensional Array
To declare a two-dimensional integer array table of size 10, 20 you would write:
int table[][] = new int[10][20];
EX:
int t, i
int table[][] = new int[3][4];
for(t = 0; t
Irregular(Ragged) Multidimensional Arrays
When you allocate memory for MULTIDIMENSIONAL array, you need to specify ONLY the memory for the FIRST (leftmost) dimension. You can ALLOCATE the REMAINING dimensions separately.
EX: int table[][] = new int[3][]; table[0] = new int[4]; table[1] = new int[4] table[2] = new int[2]
Since multidimensional arrays are implemented as arrays of arrays, the length of each array is under your control.
Arrays of 3+ Dimensions
type name[][]…[] = new type[size1][size2]…[sizeN]
Ex:
int multidim[][][] = new int[3][10][4]