Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:

Similar presentations


Presentation on theme: "1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:"— Presentation transcript:

1 1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example: drivers & trips Example: working with matrices

2 2 Arrays Previously we saw we can have an array of elements Elements of such arrays are accessed using a subscript. Example int [] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Subscripts begin at 0 month[0] is the first element and has the value 31 month[5] is the sixth element and has the value 30 All elements are of the same type – in this case int All elements can be accessed readily using a for for (int numDays:daysInMonth) System.out.println(numDays);

3 3 Arrays of Arrays When the elements of an array are themselves arrays then we have multi-dimensional arrays, or, arrays of arrays Example. Consider a table, times, that represents a times table: times 123456 1123456 224681012 3369 1518 44812162024 5123456 661218243036

4 4 Arrays of Arrays The product, 4 x 6, is in the cell found at the intersection of the 4 th row and 6 th column As a 2-dimensional array we can access this value as times[3][5] 123456 1123456 224681012 3369 1518 44812162024 5123456 661218243036 4 th row 6 th column

5 5 Arrays of Arrays We can declare this times table in Java as int[][] times ={{1, 2, 3, 4, 5, 6}, {2, 4, 6, 8, 10, 12}, {3, 6, 9, 12, 15, 18}, {4, 8, 12, 16, 20, 24}, {5, 10, 15, 20, 25, 30}, {6, 12, 18, 24, 30, 36 } } each element is an array. 1 st subscript is row. 2 nd subscript is column, or equivalently, an index into the row. All values are enclosed in { } Each row is enclosed in { } All elements are comma-separated. times[3][5] references value in the 4 th row and within the row, the 6 th entry.

6 6 Arrays of Arrays Consider the following code that prompts for 2 integers between 1 and 6 and reports their product: int[][] times ={{1, 2, 3, 4, 5, 6}, {2, 4, 6, 8, 10, 12}, {3, 6, 9, 12, 15, 18}, {4, 8, 12, 16, 20, 24}, {5, 10, 15, 20, 25, 30}, {6, 12, 18, 24, 30, 36 } }; Scanner kb = new Scanner(System.in); System.out.println("enter two integers between 1 and 6" +" and I will tell you the product"); int i = kb.nextInt(); int j = kb.nextInt(); int p = times[i-1][j-1]; System.out.println("The product of "+i+" and "+j+" is "+p);

7 7 Representation in memory The JVM stores, in the memory location of the variable times, a reference to its elements (an array of …). Each element has a reference to storage locations representing the column values that make up a row. Since these are just int s these values are stored in those locations. times 61218243036 51015202530 123456... 6 arrays of 6 elements each An array of 6 elements

8 8 Creating a 2D array As shown previously we can initialize an array of arrays. We can also declare the array and assign it values through calculations or input. Consider the following code where we declare and initialize a times table that is 10 by 10: int[][] times = new int[10][10]; for (int r= 0; r<10; r++) for (int c= 0; c< 10; c++) times[r][c] = (r+1) * (c+1); Assignment statement … the element in the r th row and c th column is set to (r+1)*(c+1)

9 9 Ragged arrays Java implements multidimensional arrays as arrays of arrays Recall that an array has a length field specifying how many elements the array contains. As arrays can be of different lengths, then for a 2D array each row can be a different size. Each array has its own length field. Question: for a 10 x 10 times table, how many length fields are there?

10 10 Ragged arrays Consider Figure 2.4 which illustrates the km driven per trip for each driver

11 11 Ragged arrays As in program DriversTrips we can initialize the array: int[][] trips ={ {25, 29, 30, 40}, {44, 25}, {22, 27, 55, 33, 80}, {55, 57, 45}, {31, 42, 49, 46} }; Row has 4 elements Row has 2 elements

12 12 Ragged arrays As in program DriversTrips we can display the trips for each driver for (int i=0; i<trips.length; i++){ System.out.print("driver: "+i+"\t"); // number of trips for ith driver // is trips[i].length for (int j=0;j<trips[i].length;j++){ System.out.print(trips[i][j]+"\t"); } System.out.println(); } Driver row i Trip column j Size of i th row ≡ number of trips for i th driver number of drivers

13 13 Ragged arrays How many kilometers has each driver driven? To calculate this we must calculate row sums. for (int i=0; i<trips.length; i++){ System.out.print("driver: "+i+"\t"); // calculate row sum for driver int sum = 0; for (int j=0;j<trips[i].length;j++){ sum += trips[i][j]; } System.out.print(sum+"\n"); }

14 14 Matrices A 2D matrix is a mathematical construct of rows and columns. For example Is a 2D matrix of 2 rows and 3 columns. Sometimes called a 2 by 3 matrix [ ] 383 512

15 15 Matrices Mathematical operations (add, subtraction, scalar multiplication, transposition, multiplication, etc.) are defined for matrices. Scalar multiplication. Consider the product c*A where c is a scalar value and A is a matrix, then c*A is computed by multiplying each element of A by the value c. [ ] 123 654 = 246 12108 2

16 16 Matrices – scalar multiplication Consider the code below that computes C=2*A. int[][] a ={ {1, 2, 3}, {6, 5, 4} }; int s = 2; int [][] c = new int[2][3]; for (int i=0; i< a.length; i++) for (int j=0; j<a[i].length; j++) c[i][j] = s *a [i][j];

17 17 Matrices - addition Matrix addition. to add two matrices of equal size we just add elements in corresponding positions. [ ] 383 512 112 332 =+ 495 844

18 18 Matrices - addition The program MatrixAddition on page 37 adds two matrices. Consider the code The code is simple enough but writing such code requires care due to the different subscripts names (i & j), the different lengths (rows & columns), and the syntax requirements of Java // C = A + B // For each c[i][j] in C // c[i][j] = a[i][j]+b[i][j] // A and B must have the same // number of rows and columns for (int i=0; i< a.length; i++) for (int j=0; j<a[i].length; j++) c[i][j] =a [i][j] + b[i][j];

19 19 Matrices - addition The program MatrixAddition on page 37 does this. Consider the code The code is simple enough but writing such code requires care due to the different subscripts (i & j), the different lengths, and the syntax requirements of Java // C = A + B // For each c[i][j] in C // c[i][j] = a[i][j]+b[i][j] // A and B must have the same // number of rows and columns for (int i=0; i< a.length; i++) for (int j=0; j<a[i].length; j++) c[i][j] =a [i][j] + b[i][j];


Download ppt "1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:"

Similar presentations


Ads by Google