Multiple Dimension Arrays Extending Java Arrays Copyright 1998

  • Slides: 13
Download presentation
Multiple Dimension Arrays Extending Java Arrays Copyright © 1998 -2013 Curt Hill

Multiple Dimension Arrays Extending Java Arrays Copyright © 1998 -2013 Curt Hill

What is a multiple dimension array? • • • An array of arrays An

What is a multiple dimension array? • • • An array of arrays An array with multiple brackets One dimension is a column or list Two dimensions is a table or rectangle Three dimensions is rectangular solid Four dimensions is hard to visualize Copyright © 1998 -2013 Curt Hill

Multidimensional arrays • Declaration and allocation: double d [ ]; i=8; j=4; d =

Multidimensional arrays • Declaration and allocation: double d [ ]; i=8; j=4; d = new double [i][j]; for(m=0; m<i; m++) for(n=0; n<j; n++) d[m][n] = m*100+n; Copyright © 1998 -2013 Curt Hill

Organization • In the above: • d is a handle to a table –

Organization • In the above: • d is a handle to a table – a two dimensioned array of doubles • d[1] is a handle to a row of doubles • d[1][1] is a double Copyright © 1998 -2013 Curt Hill

Pictures • We have almost used multiple dimension arrays in Pictures • The get.

Pictures • We have almost used multiple dimension arrays in Pictures • The get. Pixel method is a wrapper for the two dimensioned array of pixels • When we do: get. Pixel(x, y); that is similar to pixels[x][y] – Presuming an array of pixels exists in the picture Copyright © 1998 -2013 Curt Hill

More than 2 • No real restrictions on the number of dimensions, other than

More than 2 • No real restrictions on the number of dimensions, other than memory consumption – int ar[][][] = new int [4][7][11][3][5]; – Requires 4*7*11*3*5 = 4620 integers – This is 18480 bytes of storage • The size is the product of dimensions times size of individual Copyright © 1998 -2013 Curt Hill

Another View • Pictures and most applications of two dimensional arrays are rectangular •

Another View • Pictures and most applications of two dimensional arrays are rectangular • Most programming languages may only handle multiple dimensioned arrays as rectangles • Java is not most programming languages Copyright © 1998 -2013 Curt Hill

Non-Rectangularity • A two dimension array is an array of arrays • There may

Non-Rectangularity • A two dimension array is an array of arrays • There may be rows of unequal length int ar [] [] = new int[3][]; ar[0] = new int[5]; ar[1] = new int[2]; ar[2] = new int[12]; Copyright © 1998 -2013 Curt Hill

Discussion • Since a multiple dimension array is an array of arrays we may

Discussion • Since a multiple dimension array is an array of arrays we may either specify one or more sizes in the new • The command: int [][] ar = new int[K] [ ]; allocates K rows, but leaves the length of each row unspecified • Later use: ar[i] = new int [10]; to specify the row size Copyright © 1998 -2013 Curt Hill

Lengths • The length property may be applied to the entire array – The

Lengths • The length property may be applied to the entire array – The number of rows – ar. length • It may be applied to each row as well – ar[i]. length – The length of this row Copyright © 1998 -2013 Curt Hill

Number of Elements • If the array is allocated with: new type[I][K] the number

Number of Elements • If the array is allocated with: new type[I][K] the number of elements is just I*K • The number of elements is somewhat harder to compute in general: count = 0; for(int i = 0; i<ar. length; i++) count += ar[i]. length; Copyright © 1998 -2013 Curt Hill

Initialization • Brace notation may be used as well • int ar[] [] =

Initialization • Brace notation may be used as well • int ar[] [] = {{0, 1, 2, 3}, {100, 101}, {200, 201, 202, 203, 204, 205}}; • • First dimension is size 3 First row is size 4 Second row is size 2 Last row is size 6 Copyright © 1998 -2013 Curt Hill

Finally • A picture’s pixels are naturally considered a two dimension array – The

Finally • A picture’s pixels are naturally considered a two dimension array – The get. Pixel(int, int) method is a method call that looks like subscripting • Most other applications of two dimensional arrays fall into the areas of mathematics, science or engineering Copyright © 1998 -2013 Curt Hill