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

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"));

有更好的方法吗?


当前回答

public static <T> List<T> asList(T... a) {
    return new ArrayList<T>(a);
}

这是Arrays.asList的实现,因此您可以使用

ArrayList<String> arr = (ArrayList<String>) Arrays.asList("1", "2");

其他回答

Java 9有以下方法来创建不可变列表:

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

如果需要,它很容易适于创建可变列表:

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

类似的方法可用于“集”和“贴图”。

正如汤姆所说:

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的另一种好方法。

在Java 9中,我们可以很容易地在一行中初始化ArrayList:

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

or

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

Java 9的这种新方法与以前的方法相比有许多优点:

空间效率不可变性线程安全

有关更多详细信息,请参阅本文->List.of和Arrays.asList之间的区别是什么?

如果您需要大小为1的简单列表:

List<String> strings = new ArrayList<String>(Collections.singletonList("A"));

如果需要多个对象的列表:

List<String> strings = new ArrayList<String>();
Collections.addAll(strings,"A","B","C","D");

使用Eclipse集合,您可以编写以下内容:

List<String> list = Lists.mutable.with("Buenos Aires", "Córdoba", "La Plata");

您还可以更具体地了解类型,以及它们是可变的还是不可变的。

MutableList<String> mList = Lists.mutable.with("Buenos Aires", "Córdoba", "La Plata");
ImmutableList<String> iList = Lists.immutable.with("Buenos Aires", "Córdoba", "La Plata");

您也可以对套装和包进行同样的操作:

Set<String> set = Sets.mutable.with("Buenos Aires", "Córdoba", "La Plata");
MutableSet<String> mSet = Sets.mutable.with("Buenos Aires", "Córdoba", "La Plata");
ImmutableSet<String> iSet = Sets.immutable.with("Buenos Aires", "Córdoba", "La Plata");

Bag<String> bag = Bags.mutable.with("Buenos Aires", "Córdoba", "La Plata");
MutableBag<String> mBag = Bags.mutable.with("Buenos Aires", "Córdoba", "La Plata");
ImmutableBag<String> iBag = Bags.immutable.with("Buenos Aires", "Córdoba", "La Plata");

注意:我是Eclipse集合的提交人。