Home » Android » java – How to insert values in two dimensional array programmatically?

java – How to insert values in two dimensional array programmatically?

Posted by: admin June 15, 2020 Leave a comment


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
    "sgi gray 92", 
 // Shades of blue
    "dodgerblue 2", 
    "steelblue 2", 
// Shades of yellow
    "yellow 1",
    "gold 1",
    "darkgoldenrod 1", 
 // Shades of red
    "indianred 1", 
    "firebrick 1", 
How to&Answers:
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.


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

For this your declaration looks like

String[][] test = new String[4][];
test[0] = new String[3];
test[1] = new String[2];



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("sgi gray 92");

        ArrayList<String> shadesOfBlue = new ArrayList<>();
        shadesOfBlue.add("dodgerblue 2");
        shadesOfBlue.add("steelblue 2");

        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"


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");
System.out.println("Enter row");
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] +"   ");