Topic 2 - 2 Dimensional Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Topic 2 - 2 Dimensional Arrays

Description:

... arrays of arrays of arrays.multiple dimensional arrays can be ragged ... Ragged arrays are sometime useful, but normally we deal with rectangular 2D arrays ... – PowerPoint PPT presentation

Number of Views:244
Avg rating:3.0/5.0
Slides: 27
Provided by: MikeS2
Category:

less

Transcript and Presenter's Notes

Title: Topic 2 - 2 Dimensional Arrays


1
Topic 2 - 2 Dimensional Arrays
2
2D Arrays in Java
  • Arrays with multiple dimensions may be declared
    and used
  • int mat new int34
  • 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

3
Number of Rows and Columns
  • Once 2D array created access elements with 2
    subscripts
  • int mat new int34
  • 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

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 int34
  • mat23 12

column
row
5
2 Dimensional Arrays
row
0 1 2 3
column
0 1 2
0 0 0 0 0 0 0 0 0 0 0
0
This is our abstract picture of the 2D array
mat21 12
6
The Real Picture
0 1 2 3
0 0 0 0
0 1 2
0 1 2 3
0 0 0 0
mat
0 1 2 3
0 0 0 0
mat holds the memory address of an array with
3 elements. Each element holds the memory
address of an array of 4 ints
7
Arrays of Multiple Dimension
  • because multiple dimensional arrays are treated
    as arrays of arrays of arraysmultiple
    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 int5for(int i 0
i lt raggedMat.length i) raggedMati new
inti 1
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
  • mat0.length refers to the number of columns per
    row

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.

10
Example of nested for Loops
  • String temp
  • for(int i 0 i lt 4 i) for(int j 0 j lt
    3 j) temp "" i "," j " "
  • System.out.print( temp )
  • System.out.println()
  • What is the output?

outer loop
inner loop
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?

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 gt 0, mat0.length gt 0,
    mat is rectangular
  • post return maximum element in mat
  • /

13
findMax Implemented
public int findMax(int mat) int max
mat00 for(int r 0 r lt mat.length
r) for(int c 0 c lt mat0.length
c) if( matrc gt max ) max
matrc return max
14
Conway' Game of Life
  • A more complicated 2D Array problem
  • Conway's Game of Life www.math.com/students/wonder
    s/life/life.html
  • 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
  • gt 4 neighbors, organism dies (over crowding)
  • empty cell
  • 3 neighbors birth, organism born

15
Game of Life Example
"" -gt occupied, "." -gt empty, generation 0
16
Game of Life
added number of neighboring cells that are
occupied
17
Game of Life
apply all changes simultaneously, generation 1
18
Game of Life
apply all changes simultaneously, generation 2
19
Game of Life
apply all changes simultaneously, generation 3
20
Game of Life
apply all changes simultaneously, generation 4
21
Game of Life
apply all changes simultaneously, generation 5
(and so forth)
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 gt 0,
    mat0.length gt 0, world is
    rectangular
  • post return next generation of world
    based on rules of Game of Life
  • /

23
nextGen source code
public boolean nextGen(int world)
final int ROWS world.length final int COLS
world0.length boolean result new
booleanROWSCOLS int num for(int r
0 r lt ROWS r) for(int c 0 c lt
COLS c) num getNumNeighbors(worl
d, r, c) //check for alive and
survives if( worldrowcol ( num
2 num 3)
resultrowcol true // check for
birth else if( !worldrowcol
num 3) resultrowcol
true
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

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)

26
the allObjects source code
/ pre none Returns all the
objects in this environment. _at_return an
array of all the environment objects /
public Locatable allObjects()
Locatable theObjects new LocatablenumObjects(
) int tempObjectCount 0   //
Look at all grid locations. for ( int r
0 r lt numRows() r ) for ( int c
0 c lt numCols() c ) // If
there's an object at this location, // put it
in the array. Locatable obj
theGridrc
if ( obj ! null )
theObjectstempObjectCount obj
tempObjectCount
return theObjects
Write a Comment
User Comments (0)
About PowerShow.com