Initialization of an ArrayList in one line

后端 未结 30 2883
北恋
北恋 2020-11-22 01:10

I wanted to create a list of options for testing purposes. At first, I did this:

ArrayList places = new ArrayList();
places.add(\         


        
30条回答
  •  情书的邮戳
    2020-11-22 01:43

    With Guava you can write:

    ArrayList places = Lists.newArrayList("Buenos Aires", "Córdoba", "La Plata");
    

    In Guava there are also other useful static constructors. You can read about them here.

提交回复
热议问题