如何使用花括号在 Java 中定义多维数组?

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

How to define a multidimensional array in Java using curly braces?

javaarraysmultidimensional-array

提问by Green

I don't understand the behaviour of Java about arrays. It forbids to define an array in one case but allows the same definition in another.

我不明白 Java 关于数组的行为。它禁止在一种情况下定义数组,而在另一种情况下允许相同的定义。

The example from tutorial:

教程中的示例:

String[][] names = {
        {"Mr. ", "Mrs. ", "Ms. "},
        {"Smith", "Jones"}
    };
System.out.println(names[0][0] + names[1][0]);    // the output is "Mr. Smith";

My example:

我的例子:

public class User {
   private static String[][] users;
   private static int UC = 0;

   public void addUser (String email, String name, String pass) {
      int i = 0;

      // Here, when I define an array this way, it has no errors in NetBeans
      String[][] u = { {email, name, pass}, {"[email protected]", "Hyman sparrow", "12345"} };

      // But when I try to define like this, using static variable users declared above, NetBeans throws errors
      if (users == null) {
         users = { { email, name, pass }, {"one", "two", "three"} };    // NetBeans even doesn't recognize arguments 'email', 'name', 'pass' here. Why?

         // only this way works
         users = new String[3][3];
         users[i][i] = email;
         users[i][i+1] = name;
         users[i][i+2] = pass;
         UC = UC + 1;
      }
    }

The mistakes thrown by NetBeans are:

NetBeans 抛出的错误是:

illegal start of expression,

表达式的非法开始,

";" expected,

";" 预期

not a statement.

不是声明

And also it doesn't recognize arguments email, name, passin the definition of the usersarray. But recognizes them when I define uarray.

而且它不承认的论点emailnamepass在定义users数组。但是当我定义u数组时会识别它们。

What is the difference between these two definitions? Why the one works but the another one defined the same way doesn't?

这两个定义有什么区别?为什么一个有效,而另一个以相同方式定义的无效?

回答by dasblinkenlight

You need to add new String[][]before the array aggregate:

您需要new String[][]在数组聚合之前添加:

users = new String[][] { { email, name, pass }, {"one", "two", "three"} };

回答by óscar López

You can use this syntax:

您可以使用以下语法:

String[][] u = {{email, name, pass}, {"[email protected]", "Hyman sparrow", "12345"}};

Onlywhen you're declaring the matrix for the first time. It won't work for assigning values to a String[][]variable afteryou've declared it elsewhere, that's why users = ...fails. For assigning values to an already-declared String[][](or a matrix of any other type for that matter), use

当您第一次声明矩阵时。其他地方声明String[][]变量后,它无法为变量赋值,这就是users = ...失败的原因。要将值分配给已声明的String[][](或任何其他类型的矩阵),请使用

users = new String[][] { { email, name, pass }, {"one", "two", "three"} };

回答by Enrique Marcos

For re-assigning the matrix you must use new:

要重新分配矩阵,您必须使用new

users = new String[][] {{email, name, pass }, {"one", "two", "three"}};

回答by Mohammad Dehghan

The first case is an initialization statement, while the second is only an assignment. That kind of filling arrays is only supported when defining a new array.

第一种情况是初始化语句,而第二种情况只是赋值。只有在定义新数组时才支持这种填充数组。