19
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

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

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

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

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 0month[0] is the first element and has the value 31month[5] is the sixth element and has the value 30

All elements are of the same type – in this case intAll elements can be accessed readily using a for

for (int numDays:daysInMonth) System.out.println(numDays);

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

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:time

s1 2 3 4 5 6

1 1 2 3 4 5 6

2 2 4 6 8 10 12

3 3 6 9 12 15 18

4 4 8 12 16 20 24

5 1 2 3 4 5 6

6 6 12 18 24 30 36

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

4

Arrays of Arrays

The product, 4 x 6, is in the cell found at the intersection of the 4th row and 6th column

As a 2-dimensional array we can access this value as times[3][5]

1 2 3 4 5 61 1 2 3 4 5 6

2 2 4 6 8 10 12

3 3 6 9 12 15 18

4 4 8 12 16 20 24

5 1 2 3 4 5 6

6 6 12 18 24 30 36

4th row 6th column

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

5

Arrays of Arrays

We can declare this times table in Java asint[][] 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.

1st subscript is row.

2nd 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 4th row and within the row, the 6th entry.

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

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

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

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 ints these values are stored in those locations.

times

6 12 18 24 30 36

5 10 15 20 25 30

1 2 3 4 5 6

. . .6 arrays of 6 elements each

An array of 6 elements

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

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 rth row and cth column is set to (r+1)*(c+1)

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

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?

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

10

Ragged arrays

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

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

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

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

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 iTripcolumn j

Size of ith row ≡ number of trips for ith driver

number of drivers

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

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

}

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

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

[ ]3 8 35 1 2

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

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.

[ ]1 2 36 5 4 = [ ]2 4 6

12 10 82

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

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

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

17

Matrices - addition

Matrix addition.to add two matrices of equal size we just add elements in corresponding positions.

[ ]3 8 35 1 2 [ ]1 1 2

3 3 2 =+ [ ]4 9 58 4 4

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

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

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

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