用java编写二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4289276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:26:02  来源:igfitidea点击:

Writing two dimensional array in java

javaarrays

提问by sap

I have the following two dimensional array in my java program.

我的java程序中有以下二维数组。

Integer[][] arrayOfSets = {
        {1,2,5,6,9,10},
        {9,10,11,12},
        {1,2,3,4},
        {3,5,6,7,8},
        {9,10,11,12},
        {4,8},
};

Can someone show me the code on how to make the program dynamic, where the program asks the user to enter the row number and column number, i.e. how many arrays the two dimensional array should hold and then asks the user for how big each array and what are the numbers that each array should hold? Thanks so much in advance.

有人可以向我展示如何使程序动态化的代码,其中程序要求用户输入行号和列号,即二维数组应该包含多少个数组,然后询问用户每个数组有多大和每个数组应该保存的数字是多少?非常感谢。

This is what I've tried so far:

这是我迄今为止尝试过的:

 System.out.println("HOw many sets arrays would you like");
    int numArrays=sc.nextInt();
    Integer [][] arrayName = new Integer[numArrays][];
    for(int i=0;i<arrayName.length;i++){
        System.out.println("enter the size of the array number"+i);
        int sizeArray=sc.nextInt();
        for(int k=0;k<sizeArray;k++){
            System.out.println("enter element");
            int e=sc.nextInt();
            arrayName[i][k]=e;
        }

    }

Does this look right? Thanks for all the help in advance.

这看起来对吗?提前感谢所有帮助。

Ok I modified my cod again. Following is what I have now:

好的,我再次修改了我的鳕鱼。以下是我现在所拥有的:

System.out.print("How many arrays would you like?"); int numSets=sc.nextInt(); Integer[][] result = new Integer[numSets][];

System.out.print("你想要多少个数组?"); int numSets=sc.nextInt(); Integer[][] 结果 = 新的 Integer[numSets][];

for (int k=0; k< numSets; k++){
  System.out.println("Enter the size of the array");
  int setSize  = sc.nextInt();

  Integer[] row = new Integer[setSize];
  for (int m=0; m< setSize; m++){
      System.out.println("enter element: ");
      row[m] = sc.nextInt();
  }
  result[k] = row;
}

采纳答案by austinbv

You just prompt the user for the initial array sizes then initialize the array.

您只需提示用户输入初始数组大小,然后初始化数组。

import java.util.Scanner;
public class blah {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("How Many Rows?");
        int rows = s.nextInt();
        System.out.println("How Many Colums?");
        int cols = s.nextInt();
        int [][] arrayOfSets = new int [rows] [cols];
    }
}

回答by Jason S

Here's a sample function you could adapt:

这是您可以调整的示例函数:

 Integer[][] createSampleArray()
 {
      final int N1 = 5;
      Random r = new Random();
      Integer[][] result = new Integer[N1][];
      for (int i = 0; i < N1; ++i)
      {
          final int N2 = r.nextInt();
          Integer[] row = new Integer[N2];
          for (int j = 0; j < N2; ++j)
          {
              row[j] = r.nextInt();
          }
          result[i] = row;
      }
      return result;
 }


Aha, your problem is you're not creating any 1-D arrays to put the elements in:

啊哈,你的问题是你没有创建任何一维数组来放置元素:

Integer [][] arrayName = new Integer[numArrays][];
    for(int i=0;i<arrayName.length;i++){
        System.out.println("enter the size of the array number"+i);
        int sizeArray=sc.nextInt();
        for(int k=0;k<sizeArray;k++){
            System.out.println("enter element");
            int e=sc.nextInt();
            arrayName[i][k]=e;
        }

    }

You can't assign arrayName[i][k]until you assign arrayName[i]first for each row:

在为每一行先分配arrayName[i][k]之前,您无法分配arrayName[i]

arrayName[i] = new Integer[sizeArray];

arrayName[i] = new Integer[sizeArray];

回答by casablanca

What you have is almost correct. This:

你所拥有的几乎是正确的。这个:

Integer [][] arrayName = new Integer[numArrays][];

creates the first dimension, but you also need to create the individual second dimensions. After you read in the size of the each array:

创建第一个维度,但您还需要创建单独的第二个维度。读入每个数组的大小后:

int sizeArray=sc.nextInt();

you need to create the corresponding array:

您需要创建相应的数组:

arrayName[i] = new Integer[sizeArray];

回答by Raghav

@casablanca answer is perfect. You need to create the object of 'Integer' class in the end

@casablanca 的回答是完美的。最后需要创建'Integer'类的对象

 for(int j=0; j < arrayname[i].length; j++)
  arrayname[i][j] = new Integer(int_value);