Download presentation
Presentation is loading. Please wait.
1
Topic 2 - 2 Dimensional Arrays
2
2D Arrays in Java Arrays with multiple dimensions may be declared and used int[][] mat = new int[3][4]; the number of pairs of square brackets indicates the dimension of the array. could have an array of 3 dimensions, or 4 or more, but AP only covers arrays of 1 or 2 dimensions Topic 2 - 2D Arrays
3
Number of Rows and Columns
Once 2D array created access elements with 2 subscripts int[][] mat = new int[3][4]; by convention, in a 2D array the first subscript indicates the row number and the second the column number mat has 3 rows and 4 columns per row Topic 2 - 2D Arrays
4
Coordinates of a Cell When accessing elements 2 subscripts are needed
the first subscript indicates the row the second subscript indicates the column int[][] mat = new int[3][4]; mat[2][3] = 12; column row Topic 2 - 2D Arrays
5
2 Dimensional Arrays row 0 1 2 3 column 0 0 0 0 1 0 0 0 0 2
1 2 This is our abstract picture of the 2D array mat[2][1] = 12; Topic 2 - 2D Arrays
6
The Real Picture 1 2 mat mat holds the memory address of an array with 3 elements. Each element holds the memory address of an array of 4 ints Topic 2 - 2D Arrays
7
Arrays of Multiple Dimension
because multiple dimensional arrays are treated as arrays of arrays of arrays……multiple dimensional arrays can be ragged each row does not have to have the same number of columns each row array has its own length variable this is rarely used and is not tested on APCS int[][] raggedMat = new int[5][]; for(int i = 0; i < raggedMat.length; i++) raggedMat[i] = new int[i + 1]; Topic 2 - 2D Arrays
8
Rectangular 2D Arrays Ragged arrays are sometime useful, but normally we deal with rectangular 2D arrays each row has the same number of columns as every other row use this a lot as precondition to methods that work on matrices given int[][] mat mat.length refers to the number of rows in the 2D array mat[0].length refers to the number of columns per row Topic 2 - 2D Arrays
9
Working with 2D Arrays It is very common to have to access all elements of a 2D array Simplest way to do this is with a nested for loop. Topic 2 - 2D Arrays
10
Example of nested for Loops
String temp; for(int i = 0; i < 4; i++) { for(int j = 0; j < 3; j++) { temp = "[" + i + "," + j + "] "; System.out.print( temp ); } System.out.println(); What is the output? outer loop inner loop Topic 2 - 2D Arrays
11
Output of Example [0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2] [3,0] [3,1] [3,2] What would out be if the println statement were removed? Topic 2 - 2D Arrays
12
2D Array Example Find the maximum value in a 2D array of ints
must search every element public int findMax(int[][] mat) { /* pre: mat != null, mat.length > 0, mat[0].length > 0, mat is rectangular post: return maximum element in mat */ Topic 2 - 2D Arrays
13
findMax Implemented public int findMax(int[][] mat)
{ int max = mat[0][0]; for(int r = 0; r < mat.length; r++) { for(int c = 0; c < mat[0].length; c++) { if( mat[r][c] > max ) { max = mat[r][c]; } return max; Topic 2 - 2D Arrays
14
Conway' Game of Life A more complicated 2D Array problem
Conway's Game of Life cells are either occupied by an organism or empty next generation depends on the current status (occupied or empty) and your 8 neighboring cells occupied cell 0 - 1 neighbors, organism dies (loneliness) 2 - 3 neighbors, organism lives >= 4 neighbors, organism dies (over crowding) empty cell 3 neighbors birth, organism born Topic 2 - 2D Arrays
15
Game of Life Example 1 2 3 4 5 . * * * * . * * * "*" -> occupied, "." -> empty, generation 0 Topic 2 - 2D Arrays
16
Game of Life 1 2 3 4 5 . 1 * 2 * * * . 1 * * * * * added number of neighboring cells that are occupied Topic 2 - 2D Arrays
17
Game of Life 1 2 3 4 5 . * * * . * * * * * apply all changes simultaneously, generation 1 Topic 2 - 2D Arrays
18
Game of Life 1 2 3 4 5 * . * * * * * apply all changes simultaneously, generation 2 Topic 2 - 2D Arrays
19
Game of Life 1 2 3 4 5 . * * * * * * * * apply all changes simultaneously, generation 3 Topic 2 - 2D Arrays
20
Game of Life 1 2 3 4 5 * * * * * * apply all changes simultaneously, generation 4 Topic 2 - 2D Arrays
21
Game of Life 1 2 3 4 5 * * * * * * apply all changes simultaneously, generation 5 (and so forth…) Topic 2 - 2D Arrays
22
Game of Life nextGen method
Write a method that returns a 2D array of booleans that represents the next generation based on the current generation public boolean[][] nextGen(boolean[][] world) { /* pre: world != null, mat.length > 0, mat[0].length > 0, world is rectangular post: return next generation of world based on rules of Game of Life */ Topic 2 - 2D Arrays
23
nextGen source code public boolean[][] nextGen(int[][] world)
{ final int ROWS = world.length; final int COLS = world[0].length; boolean[][] result = new boolean[ROWS][COLS]; int num; for(int r = 0; r < ROWS; r++) { for(int c = 0; c < COLS; c++) { num = getNumNeighbors(world, r, c); //check for alive and survives if( world[row][col] && ( num == 2 || num == 3) result[row][col] = true; // check for birth else if( !world[row][col] && num == 3) } Topic 2 - 2D Arrays
24
2D Array in the MBCS The AP Marine Biology Case Study uses a 2D array in its BoundedEnv class the BoundedEnv class represents an area of water in which the fish in the MBCS are located instance variables for BoundedEnv private Locatable[][] theGrid; private int objectCount; Topic 2 - 2D Arrays
25
the allObjects method a method in the BoundedEnv class
returns an array with a reference to everything in the BoundedEnv typical 2D array processing (nested loop) makes use of helper methods instead of accessing length variables directly (numRows, numCols) Topic 2 - 2D Arrays
26
the allObjects source code
/** pre: none * Returns all the objects in this environment * @return an array of all the environment objects **/ public Locatable[] allObjects() { Locatable[] theObjects = new Locatable[numObjects()]; int tempObjectCount = 0; // Look at all grid locations. for ( int r = 0; r < numRows(); r++ ) { for ( int c = 0; c < numCols(); c++ ) { // If there's an object at this location, // put it in the array. Locatable obj = theGrid[r][c]; if ( obj != null ) { theObjects[tempObjectCount] = obj; tempObjectCount++; } return theObjects; Topic 2 - 2D Arrays
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.