Title: Multidimensional%20Arrays
1 2Multidimensional Arrays
- Arrays are good for storing lists of data
- What about storing a table?
14 8 9 8 7 78 90 89 70 70 7 09 09 09
79 70 87 97 07 87 89 90 7 87 7 70 87 09
70 70 7 09 09 09 89 09 89 08 77 78 8 67
87 56 65 45 30 91 70 39 37 30 37 08 37 38
89 20 89 78 27 87 24 27 78 28 27 89 78 80
80 89 89 89 89 89 78 46 45 70 20 9 7 72
3Multidimensional 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!
4Multidimensional Arrays
- 2D array declaration
- ltdata typegt ltnamegt
- int grades
- String seatingChart
- 2D array definition
- grades new int915
- seatingChart new String59
- Remember, each reference in seatingChart must be
initialized!
5Multidimensional Arrays
- Accessing elements
- grades00 90
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14
0 1 2 3 4 5 6 7
90
6Multidimensional Arrays
- Accessing elements
- grades00 90
- Grades85 65
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14
0 1 2 3 4 5 6 7
90
65
7Multidimensional Arrays
- for(int i0 ilt grades.length i)
- for(int j0 jlt gradesi.length j)
- averagesi gradesij
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14
0 1 2 3 4 5 6 7
90 34 65 87 65 78 57 98 87 65 77 88 99 43 76
78 57 98 87 65 77 90 34 65 87 65 78 57 77 88
34 65 87 65 87 65 78 57 77 87 65 78 45 76 44
57 98 87 65 34 65 87 65 78 57 98 87 65 77 88
65 77 90 34 65 87 65 78 78 57 98 87 65 77 77
87 65 78 57 77 87 76 67 89 82 92 17 95 98 87
78 57 98 87 65 77 90 34 65 87 76 67 89 82 65
87 65 78 77 87 76 67 89 82 92 17 65 77 90 34
57 98 87 65 34 65 78 57 98 87 65 77 65 34 65
8Multidimensional Arrays
- How are 2D arrays really stored?
0 1 2
0 1 2 3
90 34 65
78 57 98
34 65 87
57 98 87
9Multidimensional Arrays
- How are 2D arrays really stored?
- 2D arrays are arrays of arrays
0 1 2
0 1 2 3
90 34 65
78 57 98
34 65 87
57 98 87
10Multidimensional Arrays
- How are 2D arrays really stored?
- 2D arrays are arrays of arrays
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
11Multidimensional Arrays
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
12Multidimensional Arrays
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
13Multidimensional Arrays
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
14Multidimensional Arrays
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
15Multidimensional Arrays
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
16Multidimensional Arrays
i
j
- for(int i0 ilt grades.length i)
- for(int j0 jlt gradesi.length j)
- averagesi gradesij
0
0
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
17Multidimensional Arrays
i
j
- for(int i0 ilt grades.length i)
- for(int j0 jlt gradesi.length j)
- averagesi gradesij
0
0
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
18Multidimensional Arrays
i
j
- for(int i0 ilt grades.length i)
- for(int j0 jlt gradesi.length j)
- averagesi gradesij
0
1
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
19Multidimensional Arrays
i
j
- for(int i0 ilt grades.length i)
- for(int j0 jlt gradesi.length j)
- averagesi gradesij
0
2
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
20Multidimensional Arrays
i
j
- for(int i0 ilt grades.length i)
- for(int j0 jlt gradesi.length j)
- averagesi gradesij
1
0
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
21Multidimensional Arrays
i
j
- for(int i0 ilt grades.length i)
- for(int j0 jlt gradesi.length j)
- averagesi gradesij
1
1
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
22Multidimensional Arrays
i
j
- for(int i0 ilt grades.length i)
- for(int j0 jlt gradesi.length j)
- averagesi gradesij
1
2
0 1 2
90 34 65
0 1 2
0 1 2 3
0 1 2 3
0 1 2
90 34 65
78 57 98
34 65 87
57 98 87
78 57 98
0 1 2
34 65 87
0 1 2
57 98 87
23Multidimensional Arrays
- Although we can think of it in table format for
accessing, it is not necessarily a table! - int threeD new int10
- threeD10 new int 10
- threeD105 new int7
- threeD1054 6
- What is the Memory Diagram?
24Multidimensional Arrays
- int threeD new int10
- threeD9 new int 10
- threeD95 new int7
- threeD954 6
0 1 2 3 4 5 6 7 8 9
25Multidimensional Arrays
- int threeD new int10
- threeD9 new int 10
- threeD95 new int7
- threeD954 6
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
9
26Multidimensional Arrays
- int threeD new int10
- threeD9 new int 10
- threeD95 new int7
- threeD954 6
0 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
27Multidimensional Arrays
- int threeD new int10
- threeD9 new int 10
- threeD95 new int7
- threeD954 6
0 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
28Multidimensional Arrays
- Exercise
- Write a method that takes an int n, and returns
the multiplication table (2D array) for all
numbers from - 1 to n
29Multidimensional Arrays
- public static int multTable(int n)
- int table new intnn
- for(int i1 i lt n i)
- for(int j1 jltn j)
- tableij ij
- return table