First, understand
The two-dimensional array itself is also a reference type , and the saved address number
A two-dimensional array holds a container for multiple one-dimensional arrays, which is equivalent to the combination of multiple one-dimensional arrays, that is, each element in a two-dimensional array is a one-dimensional array
Second, the use of the array steps1. Dynamic initialization
(1) declares the
data type [] array name;
or
data type [] array name [];
or
Data type array name [];
(2) Open Space
format one: fixed number of columns
Array name = new data type [number of rows] [number of columns];//number of rows corresponds to the length of a two-d array or the number of one-dimensional arrays, Number of columns equal to the number of members in each one-dimensional array
format two: Not fixed number of columns
Array name = new data type [ number of rows ] [];
The first two steps can be merged
Syntax example:
int[][] arr = new int[5][];
(3) Assignment
Format one: fixed number of columns
for (int i=0;i< array name. length;i++) {
for (int j=0;j< array name [i].length;j++) {
Array] name [I][j] = value;
}
}
Format two: Number of non-fixed columns
for (int i=0;i< array name. length;i++) {
array name [i]=new data type [length];//length cannot be omitted!
for (int j=0;j< array name [i].length;j++) {
array name [I][J] = value;
}
}
(4) Use
for (int i=0;i< array name. length;i++) {
for (int j=0;j< array name [i].length;j++) {
Sy STEM.OUT.PRINTLN (array name [I][J]);
}
}
2. Static initialization
(1) Declaring and initializing
data type [] array name ={{value, value},{value, value, value}};
data type [] array name =new data type [][]{{value, value},{value, value, value}};
(2) Use
for (int i=0;i< array name. length;i++) {
for (int j=0;j< array name [i].length;j++) {
SYSTEM.OUT.PRINTLN (array name [I][J]);
}
}
Javase Basics (4)-Application of arrays (4.3 Two-dimensional arrays)