How to insert values โ€‹โ€‹into a two-dimensional array programmatically?

I want to do this dynamically in java. I know how to embed values โ€‹โ€‹in a one-dimensional array. I am a little confused in a 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", } }; 
+8
source share
6 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 } } 
+7
source

Try the 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; } } 
+5
source

You cannot โ€œaddโ€ values โ€‹โ€‹to the array, since the length of the array is immutable. You can set values โ€‹โ€‹at specific positions in the array.

If you know how to do this with one-dimensional arrays, you know how to do it with n-dimensional arrays: in Java there are no n-dimensional arrays, but only arrays of arrays (arrays ...).

But you can bind an index operator to access an array element.

 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"; 

Please note that the sizes of the child arrays must not match.

+1
source

Think of it as an array of arrays.

If you do this str [x] [y], then there is an array of length x, where each element in turn contains an array of length y. In java, it is not necessary that the second size have the same length. So for x = i you can have y = m and x = j, you can have y = n

To do this, your ad looks like

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

etc..

0
source

If you do not know in advance how many elements you will have to process, it is better to use collections ( https://en.wikipedia.org/wiki/Java_collections_framework ). You could also create a new larger 2-dimensional array, copy the old data and paste in new elements, but the data collection environment processes this automatically.

In this case, you can use the line map in line lists:

 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" } } 
0
source

this is the 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
source

Source: https://habr.com/ru/post/916634/


All Articles