在一行中初始化ArrayList我想为测试目的创建一个选项列表。一开始,我做了这样的事: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"));有更好的方法吗?
3 回答
慕莱坞森
TA贡献1810条经验 获得超4个赞
ArrayList
List
ArrayList<String> list = new ArrayList<String>();list.add("A");list.add("B");list.add("C");
list
ArrayList<String> list = new ArrayList<String>() {{ add("A"); add("B"); add("C");}};
ArrayList
List<String> list = ["A", "B", "C"];
List
ArrayList
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
List
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
List<String> places = Collections.singletonList("Buenos Aires");
places
UnsupportedOperationException
ArrayList
ArrayList
ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
添加回答
举报
0/150
提交
取消