Is there a way to do this in one line?

This question already has an answer here:

  • Initialization of an ArrayList in one line 32 answers

  • Arrays.asList接收省略号( T... ),所以你不需要数组文字:

    myList.addAll(Arrays.asList("Whiskey", "Tango", "Foxtrot"));
    

    myList.addAll(Arrays.asList("Whiskey", "Tango", "Foxtrot"));


    因为我不认为你需要字符串数组,所以你可以这样使用

    List<String> x = new ArrayList<String>() {{add("Whiskey");add("Tango")add("Foxtrot");}};
    
    链接地址: http://www.djcxy.com/p/27720.html

    上一篇: 无法从单独的类中检索ArrayList

    下一篇: 有没有办法在一行中做到这一点?