Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;

Similar presentations


Presentation on theme: "© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;"— Presentation transcript:

1 © 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239; cs239 = new Grade[10]; cs239[0] = new Grade(89.9); count 01234567890123456789 Grade 01234567890123456789

2 © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays of Objects Each element needs to be initialized. for (int i = 0; i < accounts.length; i++) accounts[i] = new BankAccount(); Example: ObjectArray.javaObjectArray.java The accounts variable holds the address of an BankAccount array. Address accounts[1] accounts[0] accounts[3] accounts[2] Address accounts[4] balance: 0.0 balance: 0.0

3 © 2004 Pearson Addison-Wesley. All rights reserved7-3 Two-Dimensional Arrays A one-dimensional array stores a list of elements A two-dimensional array can be thought of as a table of elements, with rows and columns one dimension two dimensions

4 © 2004 Pearson Addison-Wesley. All rights reserved7-4 Two-Dimensional Arrays To be precise, in Java a two-dimensional array is an array of arrays A two-dimensional array is declared by specifying the size of each dimension separately: int[][] scores = new int[12][50]; A array element is referenced using two index values: value = scores[3][6] The array stored in one row can be specified using one index

5 © 2004 Pearson Addison-Wesley. All rights reserved7-5 Two-Dimensional Arrays ExpressionTypeDescription tableint[][] 2D array of integers, or array of integer arrays table[5]int[] array of integers table[5][12]int integer See TwoDArray.java (page 399) TwoDArray.java See SodaSurvey.java (page 400) SodaSurvey.java

6 © 2004 Pearson Addison-Wesley. All rights reserved7-6 Parallel Arrays names[0] addresses[0] Person #1 names[1] addresses[1] Person #2 names[2] addresses[2] Person #3 names[4] addresses[4] Person #5 names[3] addresses[3] Person #4 Relationship between names and addresses array elements. Parallel arrays are useful when storing data of unlike types. Example: ParallelArrays.javaParallelArrays.java

7 © 2004 Pearson Addison-Wesley. All rights reserved7-7 Declaring a two-dimensional array requires two sets of brackets and two size declarators  The first one is for the number of rows  The second one is for the number of columns. double[][] scores = new double[3][4]; The two sets of brackets in the data type indicate that the scores variable will reference a two-dimensional array. Notice that each size declarator is enclosed in its own set of brackets. Two-Dimensional Arrays two dimensional array rowscolumns

8 © 2004 Pearson Addison-Wesley. All rights reserved7-8 Accessing Two-Dimensional Array Elements Accessing one of the elements in a two- dimensional array requires the use of both subscripts. scores[2][1] = 95; 0000 row 0 column 1column 2column 3column 0 row 1 row 2 The scores variable holds the address of a 2D array of doubles. Address 0000 00950

9 © 2004 Pearson Addison-Wesley. All rights reserved7-9 Initializing a Two-Dimensional Array Initializing a two-dimensional array requires enclosing each row’s initialization list in its own set of braces. int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Java automatically creates the array and fills its elements with the initialization values.  row 0 {1, 2, 3}  row 1 {4, 5, 6}  row 2 {7, 8, 9} Declares an array with three rows and three columns.

10 © 2004 Pearson Addison-Wesley. All rights reserved7-10 Initializing a Two-Dimensional Array 321 row 0 column 1column 2column 0 row 1 row 2 Address 654 987 The numbers variable holds the address of a 2D array of int values. int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; produces:

11 © 2004 Pearson Addison-Wesley. All rights reserved7-11 The length Field To access the length fields of the array: int[][] numbers = { { 1, 2, 3, 4 }, { 5, 6, 7 }, { 9, 10, 11, 12 } }; for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) System.out.println(numbers[row][col]); } Number of rowsNumber of columns in this row. The array can have variable length rows.

12 © 2004 Pearson Addison-Wesley. All rights reserved7-12 More Than Two Dimensions Java does not limit the number of dimensions that an array may be. More than three dimensions is hard to visualize, but can be useful in some programming problems.


Download ppt "© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;"

Similar presentations


Ads by Google