/
Notes1124.txt
55 lines (34 loc) · 964 Bytes
/
Notes1124.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Aim: How can we use multi-dimension arrays?
Do now: What can you put in arrays?
- ints
- strings
- other arrays
Multi-dimensional Arrays
An array can contain any type, including other
arrays.
An array of arrays is called a two-dimensional
array
syntax:
int[][] a2d = new int[4][5];
//instantiates a 4x5 2-D array
//The first number is the amount of arrays
//The second number is the size of each array
_ _ _ _ _ _
|_| ---> |_|_|_|_|_|
_ _ _ _ _ _
|_| ---> |_|_|_|_|_|
_ _ _ _ _ _
|_| ---> |_|_|_|_|_|
_ _ _ _ _ _
|_| ---> |_|_|_|_|_|
Finding the lengths
In the above example, a2d.length will return
4
In the above example, a2d[0].length will
return 5
2-D arrays are normally rectangular
You can use initializer lists with 2-D arrays,
this is the only way to have a jagged
(non-rectangular) 2-D array.
syntax
int[][] jag = {{1, 3, 4}, {9, 2}, {8, 6, 0, 7}}