Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two-Dimensional Arrays

Similar presentations


Presentation on theme: "Two-Dimensional Arrays"— Presentation transcript:

1 Two-Dimensional Arrays

2 Learning Objectives Introduce two-dimensional arrays
Be able to declare a two-dimensional array Be able to analyze the values in each row of a 2D array Understand the implications of an array of arrays. Describe the use of two-dimensional arrays to represent grids of information

3 Two-Dimensional Arrays
Declaration similar to one dimensional arrays Need to specify both the number of rows and columns during allocation Example: final int COLS = 6, ROWS = 5; //Constants double[][]energyTable = new double[ROWS][COLS];

4 Arrays of Arrays When we write This is shorthand for
double[][] energyTable = new double[ROWS][COLS]; This is shorthand for double[][] energyTable = new double[ROWS][]; for (int i = 0; i < ROWS; i++) energyTable[i] = new double[COLS];

5 What do you think this would do?
int [][] test = new int [3][]; test[0] = new int[5]; test[1] = new int [2]; test[2] = new int[3]; Draw a picture of the array being defined.

6 2-D Arrays: Dimensions In Java, a 2-D array is a 1-D array of 1-D arrays, its rows. Each row is stored in a separate block of consecutive memory locations. If m is a 2-D array, then m[k] is a 1-D array, the k-th row. m.length is the number of rows. m[k].length is the length of the k-th row. Nothing unusual: an array of rows contains objects; each object happens to be an array.

7 More Two D Internally, Java stores 2 dimensional arrays as an array of arrays: int [][] nums = new int[5][4]; The above is really equivalent to a 3-step process: // create the single reference nums (yellow square) int [][] nums; // create the array of references (blue squares) nums = new int[5][]; // this create the second level of arrays (red squares) for (int i=0; i < 5 ; i++) nums[i] = new int[4]; // create arrays of integers

8 Even More 2-D Note: when you initially declare a 2D array:
you must always specify the first dimension nums = new int[][]; // ILLEGAL - NEEDS 1ST DIMENSION you do not need to specify the second dimension nums = new int[5][]; // OK nums = new int[5][4]; // OK Elements of the Array: if nums is a 2D array as shown above, nums[i][j] represents a single integer in that array nums[i] represents a 1D array (a single row in the 2D array)

9 Review: Declaration int board[][] = new int[3][3];
Object sample[][] = new Object[4][4]; double trouble[][] = {{1.2, 2.3},{2.1,3.1}, {6.2, 10.0}} Draw a picture of the following array String wurds[][] = new String[5][]; for (int row=0; row<wurds.length; row++) { wurds[row]= new String[row+2]; } What is the value of wurds[2].length ? What type of value is wurds[1]? What type of value is wurds[2][1]?

10 public static void main(String [] args) { final char BLANK = '
public static void main(String [] args) { final char BLANK = '?'; final char X = 'x'; final char OH = 'o'; final int size = 3; char board[][] = new char[size][size]; for (int i=0; i < size; i++) for (int j=0; j < size; j++) board[i][j] = BLANK; for(int i=0; i< size; i++) board[i][i] = X; for (int j = 0; j< size; j++) board[j][size-j-1] = OH; System.out.print(board[i][j]+" "); System.out.println(); } Dry Run

11 Back to this example? int [][] test = new int [3][]; test[0] = new int[5]; test[1] = new int [2]; test[2] = new int[3]; How can you fill this array with random integers from 1 to 50? How can you show all of the values in the array test? How could you calculate the total for all of the values in test? How can you calculate the maximum value in the array? The maximum value from each row? Create a Java program in BlueJ to test the ideas.

12 Computing Max Values in Each Row
int [] max = new int[test.length]; for (int row = 0; row < test.length; row++) { // Set max for each row to 0 max[row] = 0; for (int column =0; column < test[row].length; column++) if (test[row][column] > max[row]) max[row] = test[row][column]; } for(int row = 0 ; row<test.length ; row++) System.out.println( row + " max value = " + max[row]);

13 Adding Another Dimension
You can create arrays of higher dimension than 2. For example, if we were measuring the temperature in a rectangular volume. int temperature[][][] = new int[10][20][30]; This creates an array of 10x20x30=6000 integers. temperature is an array of array of arrays SubArrays: temperature is a 3D array of size 10x20x30. There is one of these. temperature[i] is a 2D array of size 20x30. There are 10 of these. temperature[i][j] is a 1D array of size 30. There are 200 of these. All but the last dimension must be initially specified: int temperature[][][] = new int[10][10][]; // OK int temperature[][][] = new int[10][][]; // NOT OK

14 Program: Life The universe is a two-dimensional grid of square cells, each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated". Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: Any live cell with fewer than two live neighbors dies, as if caused by under population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

15 Patterns - Any live cell with fewer than two live neighbors dies, as if caused by under population. - Any live cell with two or three live neighbors lives on to the next generation. - Any live cell with more than three live neighbors dies, as if by overpopulation. - Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

16 Your application of Life
You determine the size of the universe Fill your universe: Examples Each cell has a 1 in N chance of being alive for the first generation. You select the value for N or have the user enter the value for N. Look up potential life patterns. Run the game for several generations. Show the universe after each generation Push: Break the program into Methods. neighbors(), showUniverse(), populate() Push: Determine a way to use Objects in your solution. Push: Have your universe ‘wrap’


Download ppt "Two-Dimensional Arrays"

Similar presentations


Ads by Google