我想创建一个用于测试的选项列表。起初,我这样做:

ArrayList<String> places = new ArrayList<String>();
places.add("Buenos Aires");
places.add("Córdoba");
places.add("La Plata");

然后,我将代码重构如下:

ArrayList<String> places = new ArrayList<String>(
    Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

有更好的方法吗?


当前回答

如果您只是将其声明为List,这会更简单——它必须是ArrayList吗?

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

或者如果您只有一个元素:

List<String> places = Collections.singletonList("Buenos Aires");

这意味着位置是不可变的(尝试更改它将导致引发UnsupportedOperationException异常)。

要创建一个可变列表,即一个具体的ArrayList,您可以从不可变列表中创建一个ArrayList:

ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

并导入正确的软件包:

import java.util.Arrays;

其他回答

(应该是评论,但太长了,所以是新回复)。正如其他人所提到的,Arrays.asList方法是固定大小的,但这并不是它唯一的问题。它也不能很好地处理继承。例如,假设您有以下内容:

class A{}
class B extends A{}

public List<A> getAList(){
    return Arrays.asList(new B());
}

上述结果导致编译器错误,因为List<B>(这是Arrays.asList返回的)不是List<a>的子类,即使您可以将类型B的object添加到List<a>object。要解决此问题,您需要执行以下操作:

new ArrayList<A>(Arrays.<A>asList(b1, b2, b3))

这可能是实现这一点的最佳方式,特别是如果您需要无边界列表或需要使用继承。

正如汤姆所说:

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

但是,既然你抱怨想要一个ArrayList,你应该首先知道ArrayList是List的一个子类,你可以简单地添加这一行:

ArrayList<String> myPlaces = new ArrayList(places);

不过,这可能会让你抱怨“表现”。

在这种情况下,这对我来说没有意义,为什么,因为您的列表是预定义的,所以它没有被定义为数组(因为在初始化时大小是已知的)。如果这是你的选择:

String[] places = {"Buenos Aires", "Córdoba", "La Plata"};

如果您不关心细微的性能差异,那么您也可以非常简单地将数组复制到ArrayList:

ArrayList<String> myPlaces = new ArrayList(Arrays.asList(places));

好吧,但未来你需要的不仅仅是地名,还需要国家代码。假设这仍然是一个预定义的列表,在运行时不会更改,那么使用枚举集是合适的,如果将来需要更改列表,则需要重新编译。

enum Places {BUENOS_AIRES, CORDOBA, LA_PLATA}

将变成:

enum Places {
    BUENOS_AIRES("Buenos Aires",123),
    CORDOBA("Córdoba",456),
    LA_PLATA("La Plata",789);

    String name;
    int code;
    Places(String name, int code) {
      this.name=name;
      this.code=code;
    }
}

枚举有一个静态值方法,该方法返回一个数组,该数组按声明顺序包含枚举的所有值,例如:

for (Places p:Places.values()) {
    System.out.printf("The place %s has code %d%n",
                  p.name, p.code);
}

在这种情况下,我想你不需要你的ArrayList。

P.S.Randyaa演示了使用静态实用程序方法Collections.addAll的另一种好方法。

这是算盘常见的代码

// ArrayList
List<String> list = N.asList("Buenos Aires", "Córdoba", "La Plata");
// HashSet
Set<String> set = N.asSet("Buenos Aires", "Córdoba", "La Plata");
// HashMap
Map<String, Integer> map = N.asMap("Buenos Aires", 1, "Córdoba", 2, "La Plata", 3);

// Or for Immutable List/Set/Map
ImmutableList.of("Buenos Aires", "Córdoba", "La Plata");
ImmutableSet.of("Buenos Aires", "Córdoba", "La Plata");
ImmutableSet.of("Buenos Aires", 1, "Córdoba", 2, "La Plata", 3);

// The most efficient way, which is similar with Arrays.asList(...) in JDK. 
// but returns a flexible-size list backed by the specified array.
List<String> set = Array.asList("Buenos Aires", "Córdoba", "La Plata");

声明:我是算盘通用的开发者。

在Java中,您无法做到

ArrayList<String> places = new ArrayList<String>( Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

如前所述,您需要执行双括号初始化:

List<String> places = new ArrayList<String>() {{ add("x"); add("y"); }};

但这可能会迫使您添加注释@SuppressWarnings(“串行”)或生成一个令人讨厌的串行UUID。大多数代码格式化程序也会将其展开为多个语句/行。

或者你也可以这样做

List<String> places = Arrays.asList(new String[] {"x", "y" });

但您可能需要执行@SuppressWarnings(“未选中”)。

此外,根据javadoc,您应该能够做到这一点:

List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

但我无法用JDK1.6编译它。

Collections.singletonList(messageBody)

如果您需要一个项目列表!

集合来自java.util包。