如何正确声明一个多维数组并给它赋值?
这是我的代码:
int x = 5;
int y = 5;
String[][] myStringArray = new String [x][y];
myStringArray[0][x] = "a string";
myStringArray[0][y] = "another string";
如何正确声明一个多维数组并给它赋值?
这是我的代码:
int x = 5;
int y = 5;
String[][] myStringArray = new String [x][y];
myStringArray[0][x] = "a string";
myStringArray[0][y] = "another string";
Java没有真正的多维数组。
例如,arr[i][j][k]
相当于 ((arr[i])[j])[k]
。换句话说,arr
只是一个数组、一个数组、一个数组。
因此,如果你知道数组如何工作,就知道多维数组如何工作了!
声明:
int[][][] threeDimArr = new int[4][5][6];
或者,使用初始化:
int[][][] threeDimArr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
访问:
int x = threeDimArr[1][0][1];
int[][] row = threeDimArr[1];
字符串表示:
Arrays.deepToString(threeDimArr);
"[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]"
尝试用以下内容替换相关行:
myStringArray[0][x-1] = "a string";
myStringArray[0][y-1] = "another string";
你的代码有误,因为子数组的长度为y
,且索引从0开始。因此,设置myStringArray [0] [y]
或myStringArray [0] [x]
将失败,因为索引x
和y
超出了范围。
String [] [] myStringArray = new String [x] [y];
是初始化矩形多维数组的正确方法。如果你想让它成为锯齿形(每个子数组可能具有不同的长度),那么你可以使用类似这个答案的代码。但请注意,在你想要一个完全矩形的多维数组的情况下,John声称必须手动创建子数组是错误的。
您还可以使用以下结构:
String[][] myStringArray = new String [][] { { "X0", "Y0"},
{ "X1", "Y1"},
{ "X2", "Y2"},
{ "X3", "Y3"},
{ "X4", "Y4"} };
你可以这样声明多维数组:
// 4 x 5 String arrays, all Strings are null
// [0] -> [null,null,null,null,null]
// [1] -> [null,null,null,null,null]
// [2] -> [null,null,null,null,null]
// [3] -> [null,null,null,null,null]
String[][] sa1 = new String[4][5];
for(int i = 0; i < sa1.length; i++) { // sa1.length == 4
for (int j = 0; j < sa1[i].length; j++) { //sa1[i].length == 5
sa1[i][j] = "new String value";
}
}
// 5 x 0 All String arrays are null
// [null]
// [null]
// [null]
// [null]
// [null]
String[][] sa2 = new String[5][];
for(int i = 0; i < sa2.length; i++) {
String[] anon = new String[ /* your number here */];
// or String[] anon = new String[]{"I'm", "a", "new", "array"};
sa2[i] = anon;
}
// [0] -> ["I'm","in","the", "0th", "array"]
// [1] -> ["I'm", "in", "another"]
String[][] sa3 = new String[][]{ {"I'm","in","the", "0th", "array"},{"I'm", "in", "another"}};
Java并不真正地支持多维数组。在Java中,二维数组只是一个数组的数组,三维数组是一个数组的数组的数组,四维数组是一个数组的数组的数组的数组,以此类推...
我们可以定义一个二维数组:
int[ ] num[ ] = {{1,2}, {1,2}, {1,2}, {1,2}}
int[ ][ ] num = new int[4][2]
num[0][0] = 1;
num[0][1] = 2;
num[1][0] = 1;
num[1][1] = 2;
num[2][0] = 1;
num[2][1] = 2;
num[3][0] = 1;
num[3][1] = 2;
If you don't allocate, let's say num[2][1]
, it is not initialized and then it is automatically allocated 0, that is, automatically num[2][1] = 0
;
Below, num1.length
gives you rows.
While num1[0].length
gives you the number of elements related to num1[0]
. Here num1[0]
has related arrays num1[0][0]
and num[0][1]
only.
Here we used a for
loop which helps us to calculate num1[i].length
. Here i
is incremented through a loop.
class array
{
static int[][] add(int[][] num1,int[][] num2)
{
int[][] temp = new int[num1.length][num1[0].length];
for(int i = 0; i<temp.length; i++)
{
for(int j = 0; j<temp[i].length; j++)
{
temp[i][j] = num1[i][j]+num2[i][j];
}
}
return temp;
}
public static void main(String args[])
{
/* We can define a two-dimensional array as
1. int[] num[] = {{1,2},{1,2},{1,2},{1,2}}
2. int[][] num = new int[4][2]
num[0][0] = 1;
num[0][1] = 2;
num[1][0] = 1;
num[1][1] = 2;
num[2][0] = 1;
num[2][1] = 2;
num[3][0] = 1;
num[3][1] = 2;
If you don't allocate let's say num[2][1] is
not initialized, and then it is automatically
allocated 0, that is, automatically num[2][1] = 0;
3. Below num1.length gives you rows
4. While num1[0].length gives you number of elements
related to num1[0]. Here num1[0] has related arrays
num1[0][0] and num[0][1] only.
5. Here we used a 'for' loop which helps us to calculate
num1[i].length, and here i is incremented through a loop.
*/
int num1[][] = {{1,2},{1,2},{1,2},{1,2}};
int num2[][] = {{1,2},{1,2},{1,2},{1,2}};
int num3[][] = add(num1,num2);
for(int i = 0; i<num1.length; i++)
{
for(int j = 0; j<num1[j].length; j++)
System.out.println("num3[" + i + "][" + j + "]=" + num3[i][j]);
}
}
}
int[][][] a = new int[4][3][2];
System.out.println(a.length); // 4
System.out.println(a[0].length); // 3
System.out.println(a[0][0].length); //2
a[0].length != a[1].length
。 int[][] myNums = { {1, 2, 3, 4, 5, 6, 7}, {5, 6, 7, 8, 9, 10, 11} };
for (int x = 0; x < myNums.length; ++x) {
for(int y = 0; y < myNums[i].length; ++y) {
System.out.print(myNums[x][y]);
}
}
输出
1 2 3 4 5 6 7 5 6 7 8 9 10 11
int[i][j][k]
的长度为i
,因此它实际上等同于一个((int[k])[j])[i]
。在声明中,大小与类型相反地书写,我想是为了使多维数组更类似于数学中的矩阵。 - Milosz