Multidimensional%20Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Multidimensional%20Arrays

Description:

Arrays are good for storing lists of data. What about storing a table? 72. 7. 9. 20. 70 ... 2D array definition: grades = new int[9][15]; seatingChart = new ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 30
Provided by: mccl8
Category:

less

Transcript and Presenter's Notes

Title: Multidimensional%20Arrays


1
  • Multidimensional Arrays

2
Multidimensional 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
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
  • ltdata typegt ltnamegt
  • int grades
  • String seatingChart
  • 2D array definition
  • grades new int915
  • seatingChart new String59
  • Remember, each reference in seatingChart must be
    initialized!

5
Multidimensional 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








6
Multidimensional 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


7
Multidimensional 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
8
Multidimensional 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
9
Multidimensional 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
10
Multidimensional 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
11
Multidimensional Arrays
  • grades01

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
12
Multidimensional Arrays
  • grades01

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
13
Multidimensional Arrays
  • grades01

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
14
Multidimensional Arrays
  • grades01

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
15
Multidimensional Arrays
  • grades01 34

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
16
Multidimensional 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
17
Multidimensional 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
18
Multidimensional 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
19
Multidimensional 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
20
Multidimensional 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
21
Multidimensional 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
22
Multidimensional 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
23
Multidimensional 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?

24
Multidimensional Arrays
  • int threeD new int10
  • threeD9 new int 10
  • threeD95 new int7
  • threeD954 6











0 1 2 3 4 5 6 7 8 9
25
Multidimensional 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

26
Multidimensional 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

27
Multidimensional 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
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 intnn
  • for(int i1 i lt n i)
  • for(int j1 jltn j)
  • tableij ij
  • return table
Write a Comment
User Comments (0)
About PowerShow.com