Multidimensional Arrays Multidimensional Arrays Arrays are good for
- Slides: 29
Multidimensional Arrays
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 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 78 46 45 70 20 9 7 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 § 2 D array declaration: § § <data type>[ ][ ] <name>; int[ ][ ] grades; String[ ][ ] seating. Chart; 2 D array definition: § § grades = new int[9][15]; seating. Chart = new String[5][9]; § Remember, each reference in seating. Chart must be initialized!
Multidimensional Arrays Accessing elements: § grades[0][0] = 90; § 0 0 1 2 3 4 5 6 7 90 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Multidimensional Arrays Accessing elements: § grades[0][0] = 90; Grades[8][5] = 65; § § 0 0 1 1 2 3 4 5 6 7 8 90 2 3 4 5 6 7 65 9 10 11 12 13 14
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 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 2 34 65 87 65 78 57 77 87 65 78 45 76 44 3 57 98 87 65 34 65 87 65 78 57 98 87 65 77 88 4 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 65 78 77 87 76 67 89 82 92 17 65 77 90 34 98 87 65 34 65 78 57 98 87 65 77 65 34 65 1 5 6 87 7 57
Multidimensional Arrays n How are 2 D arrays really stored? 0 1 2 90 34 65 78 57 98 2 34 65 87 3 57 98 87 0 1
Multidimensional Arrays n How are 2 D arrays really stored? ¡ 2 D arrays are arrays of arrays 0 1 2 90 34 65 78 57 98 2 34 65 87 3 57 98 87 0 1
Multidimensional Arrays n How are 2 D arrays really stored? ¡ 2 D arrays are arrays of arrays 0 1 2 90 34 65 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87
Multidimensional Arrays n grades[0][1] = 0 1 2 90 34 65 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87
Multidimensional Arrays n grades[0][1] = 0 1 2 90 34 65 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87
Multidimensional Arrays n grades[0][1] = 0 1 2 90 34 65 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87
Multidimensional Arrays n grades[0][1] = 0 1 2 90 34 65 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87
Multidimensional Arrays n grades[0][1] = 34 0 1 2 90 34 65 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87
Multidimensional Arrays i 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 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87 0 j 0
Multidimensional Arrays i 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 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87 0 j 0
Multidimensional Arrays i 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 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87 0 j 1
Multidimensional Arrays i 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 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87 0 j 2
Multidimensional Arrays i 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 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87 1 j 0
Multidimensional Arrays i 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 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87 1 j 1
Multidimensional Arrays i 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 78 57 98 2 34 65 3 57 98 0 1 0 0 1 2 90 34 65 0 1 2 1 78 57 98 87 2 0 1 2 87 3 34 65 87 0 1 2 57 98 87 1 j 2
Multidimensional Arrays § § Although we can think of it in table format for accessing, it is not necessarily a table! int [ ][ ][ ] three. D = new int[10][ ][ ]; three. D[10] = new int [10][ ]; three. D[10][5] = new int[7]; three. D[10][5][4] = 6; What is the Memory Diagram?
Multidimensional Arrays int [ ][ ][ ] three. D = new int[10][ ][ ]; three. D[9] = new int [10][ ]; three. D[9][5] = new int[7]; three. D[9][5][4] = 6; § 0 1 2 3 4 5 6 7 8 9
Multidimensional Arrays int [ ][ ][ ] three. D = new int[10][ ][ ]; three. D[9] = new int [10][ ]; three. D[9][5] = new int[7]; three. D[9][5][4] = 6; § 0 1 2 3 4 5 6 7 8 9
Multidimensional Arrays int [ ][ ][ ] three. D = new int[10][ ][ ]; three. D[9] = new int [10][ ]; three. D[9][5] = new int[7]; three. D[9][5][4] = 6; § 0 1 2 3 4 5 6 7 8 9 0 1 2 3 6 7 8 9 4 5 6
Multidimensional Arrays int [ ][ ][ ] three. D = new int[10][ ][ ]; three. D[9] = new int [10][ ]; three. D[9][5] = new int[7]; three. D[9][5][4] = 6; § 0 1 2 3 4 5 6 7 8 9 0 1 2 3 6 7 8 9 4 6 5 6
Multidimensional Arrays § § Exercise: Write a method that takes an int n, and returns the multiplication table (2 D array) for all numbers from 1 to n
Multidimensional Arrays public static int[ ][ ] mult. Table(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; }
- Mikael ferm
- You are good you are good when there's nothing good in me
- Zoroastrianism good thoughts good words good deeds
- Buenas tardes
- Good morning
- Good afternoon teacher
- Bris för vuxna
- Anatomi organ reproduksi
- Smärtskolan kunskap för livet
- Trög för kemist
- Argument för teckenspråk som minoritetsspråk
- Typiska drag för en novell
- Vilka tal pekar pilarna på
- Autokratiskt ledarskap
- Humanitr
- Blomman för dagen drog
- Redogör för vad psykologi är
- En lathund för arbete med kontinuitetshantering
- Bra mat för unga idrottare
- Gumman cirkel
- Publik sektor
- Etik och ledarskap etisk kod för chefer
- Datorkunskap för nybörjare
- Antikt plagg
- Orubbliga rättigheter
- Rita perspektiv
- Ministerstyre för och nackdelar
- Tack för att ni lyssnade bild
- Bästa kameran för astrofoto
- Sju principer för tillitsbaserad styrning