Download presentation
Presentation is loading. Please wait.
1
Multidimensional Arrays
2
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
3
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!
4
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!
5
Multidimensional Arrays
Accessing elements: grades[0][0] = 90; 1 2 3 4 5 6 7 90
6
Multidimensional Arrays
Accessing elements: grades[0][0] = 90; Grades[8][5] = 65; 1 2 3 4 5 6 7 90 65
7
Multidimensional Arrays
for(int i=0; i< grades.length; i++) for(int j=0; j< grades[i].length; j++) averages[i] += grades[i][j]; 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
8
Multidimensional Arrays
How are 2D arrays really stored? 1 2 3 90 34 65 78 57 98 87
9
Multidimensional Arrays
How are 2D arrays really stored? 2D arrays are arrays of arrays 1 2 3 90 34 65 78 57 98 87
10
Multidimensional Arrays
How are 2D arrays really stored? 2D arrays are arrays of arrays 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
11
Multidimensional Arrays
grades[0][1] = 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
12
Multidimensional Arrays
grades[0][1] = 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
13
Multidimensional Arrays
grades[0][1] = 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
14
Multidimensional Arrays
grades[0][1] = 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
15
Multidimensional Arrays
grades[0][1] = 34 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
16
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]; 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
17
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]; 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
18
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 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
19
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 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
20
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 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
21
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 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
22
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 90 34 65 1 2 3 1 2 3 90 34 65 78 57 98 87 78 57 98 34 65 87 57 98 87
23
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?
24
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
25
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
26
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
27
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 6
28
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
29
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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.