Multidimensional Arrays
Multidimensional Arrays Arrays are good for storing lists of data What about storing a table? 14 8 9 7 78 90 89 70 09 79 87 97 07 08 77 67 56 65 45 30 91 39 37 38 20 27 24 28 80 46 72
Multidimensional Arrays If arrays can store any object, why not have them store arrays? An array of arrays is a list of lists, or a table!
Multidimensional Arrays 2D array declaration: <data type>[ ][ ] <name>; int[ ][ ] grades; String[ ][ ] seatingChart; 2D array definition: grades = new int[9][15]; seatingChart = new String[5][9]; Remember, each reference in seatingChart must be initialized!
Multidimensional Arrays Accessing elements: grades[0][0] = 90; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 90
Multidimensional Arrays Accessing elements: grades[0][0] = 90; Grades[8][5] = 65; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 90 65
Multidimensional Arrays for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 90 34 65 87 78 57 98 77 88 99 43 76 45 44 67 89 82 92 17 95
Multidimensional Arrays How are 2D arrays really stored? 0 1 2 1 2 3 90 34 65 78 57 98 87
Multidimensional Arrays How are 2D arrays really stored? 2D arrays are arrays of arrays 0 1 2 1 2 3 90 34 65 78 57 98 87
Multidimensional Arrays How are 2D arrays really stored? 2D arrays are arrays of arrays 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays grades[0][1] = 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays grades[0][1] = 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays grades[0][1] = 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays grades[0][1] = 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays grades[0][1] = 34 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 1 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 2 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 1 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 1 1 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays j for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 1 2 0 1 2 90 34 65 0 1 2 1 2 3 1 2 3 0 1 2 90 34 65 78 57 98 87 78 57 98 0 1 2 34 65 87 0 1 2 57 98 87
Multidimensional Arrays Although we can think of it in table format for accessing, it is not necessarily a table! int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[10] = new int [10][ ]; threeD[10][5] = new int[7]; threeD[10][5][4] = 6; What is the Memory Diagram?
Multidimensional Arrays int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[9] = new int [10][ ]; threeD[9][5] = new int[7]; threeD[9][5][4] = 6; 1 2 3 4 5 6 7 8 9
Multidimensional Arrays int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[9] = new int [10][ ]; threeD[9][5] = new int[7]; threeD[9][5][4] = 6; 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
Multidimensional Arrays int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[9] = new int [10][ ]; threeD[9][5] = new int[7]; threeD[9][5][4] = 6; 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
Multidimensional Arrays int [ ][ ][ ] threeD = new int[10][ ][ ]; threeD[9] = new int [10][ ]; threeD[9][5] = new int[7]; threeD[9][5][4] = 6; 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 6
Multidimensional Arrays Exercise: Write a method that takes an int n, and returns the multiplication table (2D array) for all numbers from 1 to n
Multidimensional Arrays public static int[ ][ ] multTable(int n){ int[ ][ ] table = new int[n][n]; for(int i=1; i <= n; i++) for(int j=1; j<=n; j++) table[i][j] = i*j; return table; }