copyright © 1998-2013 curt hill multiple dimension arrays extending java arrays

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

Upload: alban-pope

Post on 05-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

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

Copyright © 1998-2013 Curt Hill

Multiple Dimension Arrays

Extending Java Arrays

Page 2: 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 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

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

Copyright © 1998-2013 Curt Hill

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;

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

Copyright © 1998-2013 Curt Hill

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

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

Pictures• We have almost used multiple

dimension arrays in Pictures• The getPixel method is a wrapper

for the two dimensioned array of pixels

• When we do:getPixel(x,y);that is similar to pixels[x][y]– Presuming an array of pixels exists in

the pictureCopyright © 1998-2013 Curt Hill

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

Copyright © 1998-2013 Curt Hill

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

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

Copyright © 1998-2013 Curt Hill

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

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

Copyright © 1998-2013 Curt Hill

Non-Rectangularity

• A two dimension array is an array of arrays

• There may be rows of unequal lengthint ar [] [] = new int[3][];ar[0] = new int[5];ar[1] = new int[2]; ar[2] = new int[12];

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

Copyright © 1998-2013 Curt Hill

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

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

Copyright © 1998-2013 Curt Hill

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

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

Copyright © 1998-2013 Curt Hill

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;

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

Copyright © 1998-2013 Curt Hill

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

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

Finally

• A picture’s pixels are naturally considered a two dimension array– The getPixel(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