Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 1998-2013 Curt Hill Multiple Dimension Arrays Extending Java Arrays."— Presentation transcript:

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

2 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

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

4 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

5 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 picture Copyright © 1998-2013 Curt Hill

6 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

7 Copyright © 1998-2013 Curt Hill A Matrix A common mathematical concept is a matrix It is a rectangular grid of values Most common usage is for manipulating simultaneous linear equations However, we will be interested in manipulating the pixels of a picture

8 Previously We have manipulated pictures with a one dimensional array of pixels This if fine for changing each pixel in some specified way It ignores lines boundaries and other important information It would be impossible to do many interesting manipulations Copyright © 1998-2013 Curt Hill

9 What sort of manipulations? Mirroring the image Finding points of high contrast Altering the size These we are familiar with by now Copyright © 1998-2013 Curt Hill

10 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

11 Copyright © 1998-2013 Curt Hill 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];

12 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

13 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

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

15 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

16 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


Download ppt "Copyright © 1998-2013 Curt Hill Multiple Dimension Arrays Extending Java Arrays."

Similar presentations


Ads by Google