8

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", 
  }
};
Cœur
  • 37,241
  • 25
  • 195
  • 267
vinothp
  • 9,939
  • 19
  • 61
  • 103

6 Answers6

13
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
     }
 }
RobinHood
  • 10,897
  • 4
  • 48
  • 97
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • How do you go about this if each index needs a different value? Wouldn't this approach make them all the same? – Jamba Mar 04 '22 at 14:52
6

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;
  }
}
openshac
  • 4,966
  • 5
  • 46
  • 77
mariomario
  • 660
  • 1
  • 9
  • 29
3

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"
    }
}
Hemaolle
  • 1,950
  • 18
  • 21
2

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.

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50
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

For this your declaration looks like

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

etc..

ejb_guy
  • 1,125
  • 6
  • 6
-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();
}
Abdullah
  • 1
  • 1
  • Welcome, you should to explain what is interesting in this answer. But o f course, you only will insert the `elem` into the last cell because of `int c = row -1` instead of `int c = 0` (same for `d`). – AxelH Dec 12 '16 at 06:55
  • And where is `size` coming from to loop on your "square array" ? – AxelH Dec 12 '16 at 07:05