Java 嗯。例如,假设您有以下内容: class A{} class B extends A{} public List<A> getAList(){ return Arrays.asList(new B()); } List<String> list = Lists.mutable.with("Buenos Aires", "Córdoba", "La Plata"); class A{} 类B扩展了{} 公共列表getAList(){ 返回Arrays.asList(新的B()); }

Java 嗯。例如,假设您有以下内容: class A{} class B extends A{} public List<A> getAList(){ return Arrays.asList(new B()); } List<String> list = Lists.mutable.with("Buenos Aires", "Córdoba", "La Plata"); class A{} 类B扩展了{} 公共列表getAList(){ 返回Arrays.asList(新的B()); },java,collections,arraylist,initialization,java-9,Java,Collections,Arraylist,Initialization,Java 9,上述操作会导致编译器错误,因为List(由array.asList返回)不是List的子类,即使您可以将类型B的对象添加到List对象中。要解决此问题,您需要执行以下操作: new ArrayList<A>(Arrays.<A>asList(b1, b2, b3)) newarraylist(Arrays.asList(b1、b2、b3)) 这可能是执行此操作的最佳方式,尤其是当您需要一个无边界列表或需要使用继承时。如: 如果您不关心性能上的细微差异,那么也可以非常简

上述操作会导致编译器错误,因为
List
(由array.asList返回)不是
List
的子类,即使您可以将类型B的对象添加到
List
对象中。要解决此问题,您需要执行以下操作:

new ArrayList<A>(Arrays.<A>asList(b1, b2, b3))
newarraylist(Arrays.asList(b1、b2、b3))
这可能是执行此操作的最佳方式,尤其是当您需要一个无边界列表或需要使用继承时。

如:

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

ArrayList<String> myPlaces = new ArrayList(Arrays.asList(places));
将成为:

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了

另一个使用静态实用程序方法的好方法。

你可以写:

ArrayList<String> places = Lists.newArrayList("Buenos Aires", "Córdoba", "La Plata");
ArrayList places=list.newArrayList(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);

在Guava中还有其他有用的静态构造函数。您可以阅读它们。

只需使用以下代码即可

List<String> list = new ArrayList<String>() {{
            add("A");
            add("B");
            add("C");
}};
List List=new ArrayList(){{
添加(“A”);
添加(“B”);
添加(“C”);
}};

集合文本没有进入Java 8,但是可以使用流API在一行相当长的行中初始化列表:

List<String> places = Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toList());
List name=Arrays.asList(“2”,“@2234”,“21”,“11”);
对于me数组。asList()是最好、最方便的。我总是喜欢这样初始化。
如果您是Java集合的初学者,那么我希望您参考一下最简洁的方法:

Double array[] = { 1.0, 2.0, 3.0};
List<Double> list = Arrays.asList(array);
双数组[]={1.0,2.0,3.0};
列表=数组。asList(数组);
使用,您可以编写以下内容:

class A{}
class B extends A{}

public List<A> getAList(){
    return Arrays.asList(new B());
}
List<String> list = Lists.mutable.with("Buenos Aires", "Córdoba", "La Plata");
List List=List.mutable.with(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
您还可以更具体地了解类型以及它们是可变的还是不可变的

MutableList<String> mList = Lists.mutable.with("Buenos Aires", "Córdoba", "La Plata");
ImmutableList<String> iList = Lists.immutable.with("Buenos Aires", "Córdoba", "La Plata");
MutableList mList=Lists.mutable.with(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
ImmutableList iList=Lists.immutable.with(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
您也可以对套装和包执行相同的操作:

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");
Set Set=Set.mutable.with(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
MutableSet mSet=Sets.mutable.with(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
ImmutableSet iSet=Sets.immutable.with(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
Bag Bag=Bags.mutable.with(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
MutableBag mBag=Bags.mutable.with(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
ImmutableBag iBag=Bags.immutable.with(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
注意:我是Eclipse集合的提交者

Collections.singletonList(messageBody)
如果您需要一个列表,请选择一项


集合来自java.util包。

请尝试使用以下代码行:

Collections.singletonList(provider)
最好的方法是:

package main_package;

import java.util.ArrayList;


public class Stackkkk {
    public static void main(String[] args) {
        ArrayList<Object> list = new ArrayList<Object>();
        add(list, "1", "2", "3", "4", "5", "6");
        System.out.println("I added " + list.size() + " element in one line");
    }

    public static void add(ArrayList<Object> list,Object...objects){
        for(Object object:objects)
            list.add(object);
    }
}
package-main\u-package;
导入java.util.ArrayList;
公共类StackKK{
公共静态void main(字符串[]args){
ArrayList=新建ArrayList();
添加(列表“1”、“2”、“3”、“4”、“5”、“6”);
System.out.println(“我在一行中添加了“+list.size()+”元素”);
}
公共静态无效添加(ArrayList、对象…对象){
用于(对象:对象)
列表。添加(对象);
}
}

只需创建一个可以包含任意多个元素的函数,并调用它将它们添加到一行中。

是的,借助数组,您可以在一行中初始化数组列表

List<String> strlist= Arrays.asList("aaa", "bbb", "ccc");
List strlist=Arrays.asList(“aaa”、“bbb”、“ccc”);
这里是代码编写者

//数组列表
列表=N.asList(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
//哈希集
Set=N.asSet(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
//哈希映射
Map Map=N.asMap(“布宜诺斯艾利斯”,1,“科尔多瓦”,2,“拉普拉塔”,3);
//或用于不可变列表/集合/映射
不可变名单(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
免疫组化(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
免疫组化(“布宜诺斯艾利斯”,1,“科尔多瓦”,2,“拉普拉塔”,3);
//最有效的方法,与JDK中的Arrays.asList(…)类似。
//但返回指定数组支持的灵活大小列表。
List set=Array.asList(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);

声明:我是AbacusUtil的开发人员。

Java 9使用以下方法创建不可变列表:

ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
List places=List.of(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);
如果需要,可轻松调整以创建可变列表:

List<String> places = new ArrayList<>(List.of("Buenos Aires", "Córdoba", "La Plata"));
List places=new ArrayList(布宜诺斯艾利斯、科尔多瓦、拉普拉塔的名单);
类似的方法可用于
Set
Map

以及以上版本,如中所建议,这可以通过使用集合文字来实现-

List<String> list = List.of("A", "B", "C");

Set<String> set = Set.of("A", "B", "C");
这与@coobird所说的类似。在JEP中也作了进一步澄清-

Map<String, String> map = Map.of("k1", "v1", "k2", "v2", "k3", "v3")

备选方案

语言变更已被多次考虑,并被拒绝:

语言 建议被搁置在一边,优先于基于图书馆的建议 总结于此

相关:

这里是另一种方式:

List<String> values = Stream.of("One", "Two").collect(Collectors.toList());
List values=Stream.of(“一”、“二”).collect(Collectors.toList());

您可以使用以下语句:

代码段:
String[]arr={“Sharlock”,“Homes”,“Watson”};
列表名称=Arrays.asList(arr);

您可以从以下位置使用粘贴列表:

列表名称=新的粘滞列表(
“斯科特·菲茨杰拉德”,“费奥多·陀思妥耶夫斯基”
);

在Java 9中,我们可以在一行中轻松初始化
数组列表

List<String> places = List.of("Buenos Aires", "Córdoba", "La Plata");
List places=List.of(“布宜诺斯艾利斯”、“科尔多瓦”、“拉普拉塔”);

List places=new ArrayList(List.of()
ArrayList<String> places = new ArrayList<String>( Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
List<String> places = new ArrayList<String>() {{ add("x"); add("y"); }};
List<String> places = Arrays.asList(new String[] {"x", "y" });
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
class A{}
class B extends A{}

public List<A> getAList(){
    return Arrays.asList(new B());
}
new ArrayList<A>(Arrays.<A>asList(b1, b2, b3))
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
ArrayList<String> myPlaces = new ArrayList(places);
String[] places = {"Buenos Aires", "Córdoba", "La Plata"};
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<String> places = Lists.newArrayList("Buenos Aires", "Córdoba", "La Plata");
List<String> list = new ArrayList<String>() {{
            add("A");
            add("B");
            add("C");
}};
List<String> places = Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toList());
ArrayList<String> places = Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toCollection(ArrayList::new));
List<String> names = Arrays.asList("2","@2234","21","11");
Double array[] = { 1.0, 2.0, 3.0};
List<Double> list = Arrays.asList(array);
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");
Collections.singletonList(messageBody)
Collections.singletonList(provider)
package main_package;

import java.util.ArrayList;


public class Stackkkk {
    public static void main(String[] args) {
        ArrayList<Object> list = new ArrayList<Object>();
        add(list, "1", "2", "3", "4", "5", "6");
        System.out.println("I added " + list.size() + " element in one line");
    }

    public static void add(ArrayList<Object> list,Object...objects){
        for(Object object:objects)
            list.add(object);
    }
}
List<String> strlist= Arrays.asList("aaa", "bbb", "ccc");
// 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");
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> list = List.of("A", "B", "C");

Set<String> set = Set.of("A", "B", "C");
Map<String, String> map = Map.of("k1", "v1", "k2", "v2", "k3", "v3")
List<String> values = Stream.of("One", "Two").collect(Collectors.toList());
String [] arr = {"Sharlock", "Homes", "Watson"};

List<String> names = Arrays.asList(arr);
List<String> names = new StickyList<>(
  "Scott Fitzgerald", "Fyodor Dostoyevsky"
);
List<String> places = List.of("Buenos Aires", "Córdoba", "La Plata");
List<String> places = new ArrayList<>(List.of("Buenos Aires", "Córdoba", "La Plata"));
static <A> ArrayList<A> ll(A... a) {
  ArrayList l = new ArrayList(a.length);
  for (A x : a) l.add(x);
  return l;
}
ArrayList<String> places = ll("Buenos Aires", "Córdoba", "La Plata");
ArrayList<String> places = Stream.of( "Buenos Aires", "Córdoba", "La Plata" ).collect( ArrayList::new, ArrayList::add, ArrayList::addAll );