ABOUTYOU
2019-06-20 10:32:30
在一行中初始化ArrayList我想為測試目的創(chuàng)建一個選項列表。一開始,我做了這樣的事:ArrayList<String> places = new ArrayList<String>();places.add("Buenos Aires");places.add("Córdoba");places.add("La Plata");然后,我將代碼重構(gòu)如下:ArrayList<String> places = new ArrayList<String>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));有更好的方法嗎?
3 回答

慕萊塢森
TA貢獻1810條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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
提交
取消