我已经看到了在Java中定义静态数组的不同方法。要么:

String[] suit = new String[] {
  "spades",
  "hearts",
  "diamonds",
  "clubs"
};

...或仅
String[] suit = {
  "spades",
  "hearts",
  "diamonds",
  "clubs"
};

或作为List
List suit = Arrays.asList(
  "spades",
  "hearts",
  "diamonds",
  "clubs"
);

有区别吗(当然除了List定义)?

有什么更好的方法(明智的选择)?

最佳答案

如果要创建数组,则没有什么区别,但是,以下内容比较整洁:

String[] suit = {
  "spades",
  "hearts",
  "diamonds",
  "clubs"
};

但是,如果要将数组传递给方法,则必须这样调用它:
myMethod(new String[] {"spades", "hearts"});

myMethod({"spades", "hearts"}); //won't compile!

关于java - 如何初始化静态数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6980199/

10-10 01:17