Download presentation
Presentation is loading. Please wait.
1
2D Arrays
2
Multiple Indices in Arrays
Arrays may have any number of indices The number of indices is the array dimension Example of declarations: // 1-dimensional array with 10 elements double oneDimArray = new double[10]; // 2-dimensional array with 3*2 = 6 elements int twoDimArray = new int[3][2]; // 3-dimensional array with 3*2*4 = 24 elements String threeDimArray = new int[3][2][4];
3
2-Dimensional Arrays Two-dimensional arrays are usually visualized as tables Really arrays of arrays Elements are organized in rows and columns Initialized and stored in row-major order, i.e., the first index represents the rows and the second index represents the columns The element [i][j] is the element at row index i and column index j
4
Initializing 2D Arrays // A 2D array with 4 rows, // 3 columns, 12 elements int[] row0 = {41,38,91}, row1 = {63,10,021}, row2 = {29,28,13}, row3 = {70,52,39}; int array[][] = {row0, row1, row2, row3}; array[0][1] array[1][0] array[1][1] array[0][0] array[0][2] array[2][0] array[2][2] array[3][0] array[1][2] array[2][1] array[3][1] array[3][2] 63 28 13 21 29 91 38 41 10 70 52 39 1 2 41 38 91 63 10 21 29 28 13 3 70 52 39 Col Index Row Index
5
Row Major Representation
So far, all 2D arrays shown have been in row major order. All of the data for the first row is stored before the next row There is also column major order You should assume all 2D arrays are row-major for our course Inner arrays represent columns 41 38 91 63 10 21 29 28 13 70 52 39 41 38 91 63 10 21 29 28 13 70 52 39
6
Row vs. Column Major int[][] rowM = {{1,2,3}, {4,5,6} };
int[][] colM = { {1,4}, {2,5}, {3,6} };
7
Accessing 2-D array elements
int[] row0 = {41,38,91}, row1 = {63,10,021}, row2 = {29,28,13}, row3 = {70,52,39}; int array[][] = {row0, row1, row2, row3}; System.out.println( array[0][2] );//91 System.out.println( array[3][1] );// array[0][1] = array[1][1] + array[2][1]; System.out.println( array[0][1] );// = 38 int rows = array.length;//number of arrays = rows int columns = array[0].length;//number of elements in an array = columns //print all indicies in the “table” (2D array) for (int rowNum = 0; rowNum < rows; rowNum++) { for (int colNum = 0; colNum < columns; colNum++) System.out.print( array[row][column] + “\t“ ); System.out.println(); }
8
Board Games One of the concepts in CS12 is to understand how we organize memory to aid in developing our solutions A natural organization for a board game is a 2D array
9
Logic – The rules Whose Turn? Is a move legal?
Check before it is allowed Is the game over? Board full No more legal moves Who is the winner? What is the score? What piece is located at a given position? Drawing/updating the board Note that all of these questions are UI independent
10
View – The Interface Views change Rules don’t
New OS New version of software Updated graphics … Rules don’t Your View of the game will be the only class that can reference any of the UI Greenfoot or Swing is fine
11
Good Design Good design requires that you keep the logic (sometimes called the document) separate from the view Your game can then be implemented on an interface relatively quickly when the underlying logic is available as a separate class When the two are tangled, making changes can be very difficult/inefficient
12
BINGO Not a two player game but let’s try to write a couple of methods to help you get the hang of it Make a new BINGO card determine if a player has a valid winning card (1 line anywhere) ? How many numbers should we expect to be called before we get a BINGO Lucky vs. Unlucky
13
BINGO How would we represent BINGO as a 2D array? B: 1-15 I: 16-30
No number can repeat
14
BINGO public static int[] fill(int from, int to) public static void shuffle(int[] arr) public static int[][] makeCard() public static void print(int[][] card) public static boolean checkVLine(int[][] card, int col, int[] called) public static boolean checkHLine(int[][] card, int row, int[] called) public static boolean checkTLtoBR(int[][] card, int[] called)//Top Left to Bottom Right public static boolean checkTRtoBL(int[][] card, int[] called)//Top Right to Bottom Left public static boolean isWinner(int[][] card, int[] called) public static int[] subArray(int[] arr, int from, int to)
15
Bingo Helpers public static int[] fill(int from, int to) { int[] arr = new int[to – from + 1]; for(int i = from; i <= to; i++) arr[i - from] = i; return arr; } public static void shuffle(int[] arr) for(int i = arr.length-1; i > 0; i--) int target = (int)(Math.random()*(i+1)); int temp = arr[i]; arr[i] = arr[target]; arr[target] = temp;
16
Bingo public static int[][] makeCard() { int rows = 5; int cols = 5;
int[][] bingo = new int[rows][cols]; int start = 1; int end = 15; int column = 0; while(end <= 75) int[] temp = fill(start, end); shuffle(temp); for(int i = 0; i < rows; i++) bingo[i][column] = temp[i]; column++; start = end + 1; end += 15; } bingo[rows/2][cols/2] = 0;//free space, sentinel value return bingo;
17
BINGO public static void print(int[][] card) { int rows = card.length; int cols = card[0].length; for(int r = 0; r < rows; r++) for(int c = 0; c < cols; c++) System.out.print(card[r][c] + “\t”); System.out.println(); }
18
BINGO Lines public static boolean checkVLine(int[][] card, int col, int[] called) { int rows = card.length; int r = 0; while(r < rows) if(!(r == rows/2 && col == cols/2))//not free square boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[r][col] == called[n]; if(!found)//card[r][col] does not contain a called number return false; } r++; return true;//all numbers were called or false would be returned
19
BINGO Lines public static boolean checkHLine(int[][] card, int row, int[] called) { int cols = card.length; int c = 0; while(c < cols) if(!(row == rows/2 && c == cols/2))//not free square boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[row][c] == called[n]; if(!found)//card[row][c] does not contain a called number return false; } c++; return true;//all numbers were called or false would be returned
20
BINGO Lines public static boolean checkTLtoBR(int[][] card, int[] called)//Top Left to Bottom Right { int rows = card[0].length; int cols = card.length; int r = 0; int c = 0; while(r < rows && c < cols) if(!(r == rows/2 && c == cols/2))//not free square boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[r][c] == called[n]; if(!found)//card[r][c] does not contain a called number return false; } r++; c++; return true;//all numbers called
21
BINGO Lines public static boolean checkTRtoBL(int[][] card, int[] called)//Top Right to Bottom Left { int rows = card[0].length; int cols = card.length; int r = 0; int c = cols - 1; while(r < rows && c >= 0) if(!(r == rows/2 && c == cols/2))//not free square boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[r][c] == called[n]; if(!found)//card[r][c] does not contain a called number return false; } r++; c--; return true;//all numbers called
22
BINGO – Better Line public static boolean checkLine(int[][] card, int[] called, int r, int c, int dr, int dc) { int rows = card[0].length; int cols = card.length; while(r < rows && r >= 0 && c < cols && c >= 0)//in bounds if(!(r == rows/2 && c == cols/2))//not free square boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[r][c] == called[n]; if(!found)//card[r][c] does not contain a called number return false; } r += dr; c += dc; return true;//all numbers called
23
BINGO public static boolean isWinner(int[][] card, int[] called) { boolean makesLine = false; for(int r = 0; r < card.length && !makesLine; r++)//horizontal lines makesLine = checkLine(card, called, r, 0, 0, 1); for(int c = 0; c < card[0].length && !makesLine; c++)//vertical lines makesLine = checkLine(card, called, 0, c, 1, 0); if(!makesLine) makesLine = checkLine(card, called, 0,0,1,1) || checkLine(card, called, 0, card[0].length, 1,-1); return makesLine; }
24
BINGO - Main public static int[] subArray(int[] arr, int from, int to) { int[] sub = new int[to-from+1]; for(int i = from; i <= to; i++) sub[i-from] = arr[i]; return sub; }
25
BINGO Main public static void main(String[] args) { int trials = 1000; int numbersCalled = 0; int counter = 0; while(counter < trials) int[][] card = makeCard(); int[] called = fill(1,75); shuffle(called); int game = 0; while(!isWinner(card, subArray(called, 0, game) )) game++; numbersCalled += game + 1;//game is 0-based counter++; } double ave = (double)numbersCalled/trials; System.out.println(ave+ “ = average number of call outs to win”); }
26
Expectations Fluency in manipulating a 2D array
Looping over/through values in the array In distinctly different ways (scrolling vertically vs horizontally is not a different way) Scanning board for score Scanning board to enforce rules Scanning board to make changes Accessing elements of the array You will be expected to transfer your skills to a similar situation, that uses a 2D array for your unit test
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.