简体   繁体   English

如何以编程方式在二维数组中插入值?

[英]How to insert values in two dimensional array programmatically?

I want to do this dynamically in java. 我想在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", 
  }
};
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...). 如果您知道如何使用一维数组,那么您就知道如何使用n维数组:Java中没有n维数组,只有数组数组(数组......)。

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. 如果你这样做str [x] [y],那么有一个长度为x的数组,其中每个元素又包含长度为y的数组。 In java its not necessary for second dimension to have same length. 在java中,第二维的长度不必相同。 So for x=i you can have y=m and x=j you can have y=n 所以对于x = i,你可以得到y = m和x = j,你可以得到y = n

For this your declaration looks like 为此您的声明看起来像

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

etc.. 等等..

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 ). 如果您事先不知道需要处理多少元素,则可能是使用集合的更好解决方案( 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"
    }
}

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM