7.1 Arrays. 7.1.1 Introduction to arrays Any array is a collection of objects or primitives Useful...
-
Upload
irene-eaton -
Category
Documents
-
view
223 -
download
0
Transcript of 7.1 Arrays. 7.1.1 Introduction to arrays Any array is a collection of objects or primitives Useful...
7.1 Arrays
7.1.1 Introduction to arrays • Any array is a collection of objects or
primitives• Useful when the number of reference variables
is large or can’t be determined until Runtime
7.1.2 Variables in arrays • An array is a collection of variables• The variables are either a primitive or a
reference to an object• There is also a reference to the array itself
7.1.3 Index
• Arrays are a built-in object• All members of the array are of the same type and size• Accessing array members is quick and efficient
7.1.4 Arrays of primitives
• This example shows two arrays of primitives
7.1.5 Arrays of objects
• Arrays of objects are a collection of references
• The array variable references the array object
• Each member of the array is itself a reference to another object
7.2.1 Creating arrays
• Step 1: Declare the array reference variableint[] arrayOfInts;
int arrayOfInts[];
• Step 2: Create the array using the ‘new’ operatorarrayOfInts = new int[5];
• The array is then initialized to default values
7.2.2 Use subscripts to access elements of an array
• Access the members of the array using an index
• Index is an int• The index for the first member is 0• The following code creates an array of ints,
then assigns a value to the 3rd element
Int[] myArray;
myArray = new int[5];
myArray[2] = 57;
7.3.1 Array of primitives
• Arrays of primitives can be assigned initial values when they are declared
• The array members can still be changed if necessary
7.3.2 Array of objects
• Initializing arrays of objects requires that the objects be instantiated first, ie:
Account[] accountList;
accountList = new Account[2];
accountList[0] = new Account(203.50);
accountList[1] = new Account(4500.00);
7.4.1 Accessing array elements
• The index value must resolve to an int
• Can be an explicit value of the result of a calculationloopValue = arrayOfInts[index – 1];
• System.arraycopy() can be used to copy the contents of one array to anotherSystem.arraycopy(arrayOfInts, 0, anotherArrayOfInts, 0,
arrayOfInts.length);
7.4.2 Using arrays
• An array can be a way of passing a large number of arguments to a method
• Parallel arrays can be used to store related informationString names = new String[20];
String addresses = new String[20];
String phone = new String[20];
Char grades = new char[20];
• Array members can be searched and sorted
7.5.1 Multidimensional arrays
• Multidimensional arrays can be used instead of parallel arrays
• Declared with multiple sets of ‘[]’int[][] multiArray = new int[20][30];
• Use a separate index for each ‘dimension’loopValue = multiArray[5][20];
7.5.2 Traversing a multidimensional array
• Nested ‘for’ loops can be used to index all the elements of an array
public int[][] myArray = new int[4][5];
for(x = 0; x < 4; x++) {
for(y = 0; y < 5; y++) {
myArray[x][y] = x * y;
}
}