Java将二维数组传递给方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20329597/
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-13 01:07:25  来源:igfitidea点击:

Java passing 2 dimension array to method

javaarraysmultidimensional-arrayparameter-passing

提问by Wizard

public static void main{
    String [][] book = new String[100][6];

    for(int i = 0; i < 1; i++) {
        for(int j = 0; j < 5; j++) {
            book[i][j] = i;
        }
    }

    arrayMethod(book);
}

public static void arrayMethod(String[][] array){
    System.out.println(Arrays.asList(array));
}

arrayMethod method output is[[Ljava.lang.String;@639facbc, [Ljava.lang.String;@8059dbd, [Ljava.lang.String;@28b6e768, [Ljava.lang.String;@1271ba, ....

arrayMethod 方法输出为[[Ljava.lang.String;@639facbc, [Ljava.lang.String;@8059dbd, [Ljava.lang.String;@28b6e768, [Ljava.lang.String;@1271ba, ....

Problem is that in arrayMethod I can't acces 2 dimension array data, where can be problem?

问题是在 arrayMethod 中我不能访问二维数组数据,哪里有问题?

采纳答案by chrylis -cautiouslyoptimistic-

It's doing exactly what you want: you're pretending the (first-level) array is a List(of Array) and then printing the toString()of those, which looks something like [Ljava.lang.String@pointer. You probably want this instead:

它正在做你想做的事情:你假装(第一级)数组是一个List(of Array),然后打印toString()那些看起来像[Ljava.lang.String@pointer. 你可能想要这个:

System.out.println(Arrays.deepToString(array));

回答by Fr0z3n7

as Alya'a Gamal said, if you want to put an int inside an array of String you need to parse it : book[i][j] = Integer.toString(i);. Then if you want to display your array, you need to run thought it, like this for example :

正如 Alya'a Gamal 所说,如果你想把一个 int 放在一个 String 数组中,你需要解析它:book[i][j] = Integer.toString(i);。然后如果你想显示你的数组,你需要运行thought it,例如:

public static void arrayMethod(String[][] array){
        for(int i = 0; i < array.length;i++) {
            for(int j = 0; j < array[i].length;j++)
                System.out.println(array[i][j]); // a stringBuilder would be better than to print inside the loop
        }

    }

回答by MouseLearnJava

You can use Arrays.toStringto print 1-D string array, but you CANNOTuse Arrays.toStringto print the 2-D array directly.

可以用于Arrays.toString打印一维字符串数组,但不能用于Arrays.toString直接打印二维数组。

There are 2 ways for you to print the 2D string array in console.

有两种方法可以在控制台中打印 2D 字符串数组。

Way#1 ==>

方式#1 ==>

System.out.println(Arrays.deepToString(array));

Way#2 ==>

方式#2 ==>

 for(String[] arr: array)
 {
        System.out.println(Arrays.toString(arr));
 }

回答by Sameer Sawla

I see three issues here :

我在这里看到三个问题:

(1). The signature of the main method looks odd. It would raise a compile issue.

(1). main 方法的签名看起来很奇怪。它会引发编译问题。

public static void main(String args[])
{
// Your code here
}

(2). In the following code :

(2). 在以下代码中:

for(int i = 0; i < 1; i++) {
  for(int j = 0; j < 5; j++) {
            book[i][j] = i;
        }
    }

book[i][j] =i; // Here you are trying to insert an int in place where a String is required.

This will again lead to a compile time issue.

这将再次导致编译时问题。

You can correct it as follows:

您可以按如下方式更正:

 book[i][j] = Integer.toString(i);

(3).

(3).

Use the following static method in the Arraysclass to print the elements of 2D array on the console.

Arrays类中使用以下静态方法在控制台上打印二维数组的元素。

System.out.println(Arrays.deepToString(array));

Hope this helps.

希望这可以帮助。

+1 for isolating the problem.

+1 用于隔离问题。