Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 7.1 Arrays

2 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

3 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

4 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

5 7.1.4 Arrays of primitives This example shows two arrays of primitives

6 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 7.2.1 Creating arrays Step 1: Declare the array reference variable int[] arrayOfInts; int arrayOfInts[]; Step 2: Create the array using the ‘new’ operator arrayOfInts = new int[5]; The array is then initialized to default values

8 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 3 rd element Int[] myArray; myArray = new int[5]; myArray[2] = 57;

9 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

10 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);

11 7.4.1 Accessing array elements The index value must resolve to an int Can be an explicit value of the result of a calculation loopValue = arrayOfInts[index – 1]; System.arraycopy() can be used to copy the contents of one array to another System.arraycopy(arrayOfInts, 0, anotherArrayOfInts, 0, arrayOfInts.length);

12 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 information String 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

13 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];

14 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; }


Download ppt "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."

Similar presentations


Ads by Google