Arrays Chapter 7 Part 3 Multi-Dimensional Arrays Chapter 7: Arrays Slide # 1
Agenda Array Dimensions Multi-dimensional arrays Basics Printing elements Chapter 7: Arrays Slide # 2
First Some Video Tutorials Matthew Guarnotta: 2D arrays in Java (9:36) CompSci Studio: Java Beginner Programming Tutorial 34 2D Arrays (6:43) Java Beginner Programming Tutorial 35 2D Array Grid (7:46) in28minutes: Java 2d array tutorial (9:37) MargretPosch Java Basics 2D Arrays (14:39) 2D + Jagged arrays + Arrays class + tostring to print elements Jose Vidal: Java Tic Tac Toe Board Program: 2D Array: Tutorial (8:38) Advanced topics: Uses tostring() method, for each loops, constructors, & enum Chapter 7: Arrays Slide # 3
Array Dimensions The arrays we discussed so far are called one-dimensional arrays Syntax review: 1. Declaring and then creating memory datatype[ ] arrayname; arrayname = new datatype[arraysize]; 2. Declaring and creating memory in one step datatype[ ] = new datatype[arraysize]; Chapter 7: Arrays Slide # 4
Array Dimensions An array that has more than one index is called multi-dimensional arrays An array that has two indexes is called twodimensional arrays (2D-Arrays or matrix) A 2D-array can be displayed as a twodimensional table 2D arrays are commonly used in financial and scientific applications Chapter 7: Arrays Slide # 5
2D Array Declaration Syntax : 1. Declaring and then creating memory datatype[][] arrayname; arrayname = new datatype[rowsize][columnsize]; 2. Declaring and creating memory in one step datatype[][] arrayname = new datatype[rowsize][columnsize]; Chapter 7: Arrays Slide # 6
2D Arrays Example Assume you want to keep the medal count for a specific Olympics How would you create a 2D array that resembles this? Gold Silver Bronze Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 Chapter 7: Arrays Slide # 7
2D Arrays Example Array declaration int[][] medals = new int[5][3]; or Array initialization during declaration int[][] medals = { {1,1,0}, {1,0,1}, Gold Silver Bronze Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 } {1,2,0}, {2,1,1}, {3,2,1} Chapter 7: Arrays Slide # 8
2D Arrays Example 0 1 2 Gold Silver Bronze 0 1 2 3 4 Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 Accessing array elements How many silver medals did Germany get? System.out.printf( Germany s medals: %d, medals[2][1]); Change the number of gold medals for Canada to be 2 medals [0][0] = 2; Chapter 7: Arrays Slide # 9
2D Arrays Example 0 1 2 Gold Silver Bronze 0 1 2 3 4 Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 To get the number of rows & columns, use the length instance variable To return the number of rows System.out.print(medals.length); To return the number of columns System.out.print(medals[anyRow].length); Chapter 7: Arrays Slide # 10
2D Arrays Example Gold Silver Bronze Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 Chapter 7: Arrays Slide # 11
2D Arrays Exercise Modify the code as follows: Add two, one-dimensional arrays to save the countries and medal names Display the countries to the left and the medal names at the top of the array contents The output should look like the figure Gold Silver Bronze Canada 1 1 0 China 1 0 1 Germany 1 2 0 Russia 2 1 1 USA 3 2 1 Chapter 7: Arrays Slide # 12
2D Arrays Exercise How will this array be populated? int my2darray [][] = new int [5][3]; for (int row = 0; row < my2darray.length; ++row) for (int col = 0; col < my2darray[row].length; ++col) my2darray[row][col] = row + col; Chapter 7: Arrays Slide # 13
2D Arrays Exercise How will this array be populated? int my2darray [][] = new int [5][3]; for (int row = 0; row < my2darray.length; ++row) for (int col = 0; col < my2darray[row].length; ++col) my2darray[row][col] = row + col; 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 14
Printing a 2D Arrays for (int row = 0; row < my2darray.length; ++row) { for (int col = 0; col < my2darray[row].length; ++col) System.out.print(my2DArray[row][col]+ \t ); System.out.println(); } 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 15
Printing a 2D Arrays Exercise: What is printed if we switch the two for statements? What must be changed in the code? 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 16
Printing a 2D Arrays Exercise: What is printed if we switch the two for statements? What must be changed in the code? 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 17
Printing a 2D Arrays Exercise: What is printed if we switch the two for statements? for (int col = 0; col < my2darray[1].length; ++col) { for (int row = 0; row < my2darray.length; ++row) System.out.print(my2DArray[row][col] + "\t"); System.out.println(); } 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 18
Printing a 2D Arrays Example: Add rows contents for (int row = 0; row < my2darray.length; ++row) { int sum = 0; for (int col = 0; col < my2darray[row].length; ++col) sum += my2darray[row][col]; System.out.println( Row + row + : + sum); } 0 1 2 0 0 1 2 1 1 2 3 2 2 3 4 3 3 4 5 4 4 5 6 Chapter 7: Arrays Slide # 19
More Array Dimensions You can define three-dimensional arrays or n- dimensional array (can be any number). Syntax: datatype[]...[] arrayname = new datatype[idx 1 ]...[idx n ]; Chapter 7: Arrays Slide # 20
More Array Dimensions Example: An array of grades, where there are 40 students, all taking the same 3 courses, and each course has 10 quizzes int [][][] grades = new int[4][3][2]; Total # element = 4 x 3 x 2 = 24 Read and fill the array of grades What could be a better names for the indices? int [][][] grades = new int[4][3][2]; // Read from the user for (int i = 0; i < 4; ++i) for (int j = 0; j < 3; ++j) for (int k = 0; k < 2; ++k) grades[i][j][k] = input.nextint(); Chapter 7: Arrays Slide # 21
3-D Array Exercise Create a program that will enter the values and displays the contents of the 3-D array. Sample Output: Chapter 7: Arrays Slide # 22
Review: Multidimensional-Array Basics Consider this table of values Chapter 7: Arrays Slide # 23
Review: Multidimensional-Array Basics We specify the row and column indices for the array to access a specific element table[3][2] has a value of 1262 Chapter 7: Arrays Slide # 24
Review: Multidimensional-Array Basics We can access elements of the table with a nested for loop Example: Chapter 7: Arrays Slide # 25
Multidimensional- Array Example Sample screen output Chapter 7: Arrays Slide # 26
Summary Multidimensional arrays are implemented as an array of arrays Treat two-dimensional array as a table with rows and columns Chapter 7: Arrays Slide # 27