How to insert values in two dimensional array programmatically?
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
}
}
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;
}
}
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...).
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.