How to insert values in two dimensional array programmatically?
Solution 1
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
}
}
Solution 2
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;
}
}
Solution 3
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.
Solution 4
In case you don't know in advance how many elements you will have to handle it might be a better solution to use collections instead (https://en.wikipedia.org/wiki/Java_collections_framework). It would be possible also to create a new bigger 2-dimensional array, copy the old data over and insert the new items there, but the collection framework handles this for you automatically.
In this case you could use a Map of Strings to Lists of Strings:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyClass {
public static void main(String args[]) {
Map<String, List<String>> shades = new HashMap<>();
ArrayList<String> shadesOfGrey = new ArrayList<>();
shadesOfGrey.add("lightgrey");
shadesOfGrey.add("dimgray");
shadesOfGrey.add("sgi gray 92");
ArrayList<String> shadesOfBlue = new ArrayList<>();
shadesOfBlue.add("dodgerblue 2");
shadesOfBlue.add("steelblue 2");
shadesOfBlue.add("powderblue");
ArrayList<String> shadesOfYellow = new ArrayList<>();
shadesOfYellow.add("yellow 1");
shadesOfYellow.add("gold 1");
shadesOfYellow.add("darkgoldenrod 1");
ArrayList<String> shadesOfRed = new ArrayList<>();
shadesOfRed.add("indianred 1");
shadesOfRed.add("firebrick 1");
shadesOfRed.add("maroon 1");
shades.put("greys", shadesOfGrey);
shades.put("blues", shadesOfBlue);
shades.put("yellows", shadesOfYellow);
shades.put("reds", shadesOfRed);
System.out.println(shades.get("greys").get(0)); // prints "lightgrey"
}
}
vinothp
Do what you like. Believe in what you do. Young and energetic individual with great passion on Mobile Application Development. Currently working as a full time mobile application developer for Android and iOS. In free time working on personal projects as well. My First Personal App - Location Plotter/ House Viewing
Updated on July 30, 2022Comments
-
vinothp almost 2 years
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.
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", } };