![Page 1: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/1.jpg)
7.1 Arrays
![Page 2: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/2.jpg)
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
![Page 3: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/3.jpg)
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
![Page 4: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/4.jpg)
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
![Page 5: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/5.jpg)
7.1.4 Arrays of primitives
• This example shows two arrays of primitives
![Page 6: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/6.jpg)
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
![Page 7: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/8.jpg)
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;
![Page 9: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/9.jpg)
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
![Page 10: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/10.jpg)
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);
![Page 11: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/11.jpg)
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);
![Page 12: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/12.jpg)
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
![Page 13: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/13.jpg)
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];
![Page 14: 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](https://reader036.vdocuments.mx/reader036/viewer/2022062320/56649d7d5503460f94a5f9f1/html5/thumbnails/14.jpg)
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;
}
}