I want to do this dynamically in java. I know how to insert values in single dimensional array. I am bit confused in two dimensional array.
我想在java中动态执行此操作。我知道如何在单维数组中插入值。我在二维数组中有点困惑。
static final String shades[][] = {
// Shades of grey
{
"lightgrey",
"dimgray",
"sgi gray 92",
},
// Shades of blue
{
"dodgerblue 2",
"steelblue 2",
"powderblue",
},
// Shades of yellow
{
"yellow 1",
"gold 1",
"darkgoldenrod 1",
},
// Shades of red
{
"indianred 1",
"firebrick 1",
"maroon",
}
};
5 个解决方案
#1
7
String[][] shades = new String[intSize][intSize];
// print array in rectangular form
for (int r=0; r<shades.length; r++) {
for (int c=0; c<shades[r].length; c++) {
shades[r][c]="hello";//your value
}
}
#2
4
Try to code below,
尝试下面的代码,
String[][] shades = new String[4][3];
for(int i = 0; i < 4; i++)
{
for(int y = 0; y < 3; y++)
{
shades[i][y] = value;
}
}
#3
1
You can't "add" values to an array as the array length is immutable. You can set values at specific array positions.
您不能将值“添加”到数组,因为数组长度是不可变的。您可以在特定阵列位置设置值。
If you know how to do it with one-dimensional arrays then you know how to do it with n-dimensional arrays: There are no n-dimensional arrays in Java, only arrays of arrays (of arrays...).
如果您知道如何使用一维数组,那么您就知道如何使用n维数组:Java中没有n维数组,只有数组数组(数组......)。
But you can chain the index operator for array element access.
但是您可以链接索引运算符以进行数组元素访问。
String[][] x = new String[2][];
x[0] = new String[1];
x[1] = new String[2];
x[0][0] = "a1";
// No x[0][1] available
x[1][0] = "b1";
x[1][1] = "b2";
Note the dimensions of the child arrays don't need to match.
请注意,子数组的维度不需要匹配。
#4
0
Think about it as array of array.
把它想象成数组的数组。
If you do this str[x][y], then there is array of length x where each element in turn contains array of length y. In java its not necessary for second dimension to have same length. So for x=i you can have y=m and x=j you can have y=n
如果你这样做str [x] [y],那么有一个长度为x的数组,其中每个元素又包含长度为y的数组。在java中,第二维的长度不必相同。所以对于x = i,你可以得到y = m和x = j你可以得到y = n
For this your declaration looks like
为此您的声明看起来像
String[][] test = new String[4][]; test[0] = new String[3]; test[1] = new String[2];
String [] [] test = new String [4] []; test [0] = new String [3]; test [1] = new String [2];
etc..
等等..
#5
-1
this is output of this program
这是该程序的输出
Scanner s=new Scanner (System.in);
int row, elem, col;
Systm.out.println("Enter Element to insert");
elem = s.nextInt();
System.out.println("Enter row");
row=s.nextInt();
System.out.println("Enter row");
col=s.nextInt();
for (int c=row-1; c < row; c++)
{
for (d = col-1 ; d < col ; d++)
array[c][d] = elem;
}
for(c = 0; c < size; c++)
{
for (d = 0 ; d < size ; d++)
System.out.print( array[c] [d] +" ");
System.out.println();
}
#1
7
String[][] shades = new String[intSize][intSize];
// print array in rectangular form
for (int r=0; r<shades.length; r++) {
for (int c=0; c<shades[r].length; c++) {
shades[r][c]="hello";//your value
}
}
#2
4
Try to code below,
尝试下面的代码,
String[][] shades = new String[4][3];
for(int i = 0; i < 4; i++)
{
for(int y = 0; y < 3; y++)
{
shades[i][y] = value;
}
}
#3
1
You can't "add" values to an array as the array length is immutable. You can set values at specific array positions.
您不能将值“添加”到数组,因为数组长度是不可变的。您可以在特定阵列位置设置值。
If you know how to do it with one-dimensional arrays then you know how to do it with n-dimensional arrays: There are no n-dimensional arrays in Java, only arrays of arrays (of arrays...).
如果您知道如何使用一维数组,那么您就知道如何使用n维数组:Java中没有n维数组,只有数组数组(数组......)。
But you can chain the index operator for array element access.
但是您可以链接索引运算符以进行数组元素访问。
String[][] x = new String[2][];
x[0] = new String[1];
x[1] = new String[2];
x[0][0] = "a1";
// No x[0][1] available
x[1][0] = "b1";
x[1][1] = "b2";
Note the dimensions of the child arrays don't need to match.
请注意,子数组的维度不需要匹配。
#4
0
Think about it as array of array.
把它想象成数组的数组。
If you do this str[x][y], then there is array of length x where each element in turn contains array of length y. In java its not necessary for second dimension to have same length. So for x=i you can have y=m and x=j you can have y=n
如果你这样做str [x] [y],那么有一个长度为x的数组,其中每个元素又包含长度为y的数组。在java中,第二维的长度不必相同。所以对于x = i,你可以得到y = m和x = j你可以得到y = n
For this your declaration looks like
为此您的声明看起来像
String[][] test = new String[4][]; test[0] = new String[3]; test[1] = new String[2];
String [] [] test = new String [4] []; test [0] = new String [3]; test [1] = new String [2];
etc..
等等..
#5
-1
this is output of this program
这是该程序的输出
Scanner s=new Scanner (System.in);
int row, elem, col;
Systm.out.println("Enter Element to insert");
elem = s.nextInt();
System.out.println("Enter row");
row=s.nextInt();
System.out.println("Enter row");
col=s.nextInt();
for (int c=row-1; c < row; c++)
{
for (d = col-1 ; d < col ; d++)
array[c][d] = elem;
}
for(c = 0; c < size; c++)
{
for (d = 0 ; d < size ; d++)
System.out.print( array[c] [d] +" ");
System.out.println();
}