1. 程式人生 > >java 設定list中陣列的值

java 設定list中陣列的值

String a="白龍馬", b="沙和尚", c="八戒", d="唐僧", e="悟空";

List<String> people=new ArrayList<>();

people.add(a);

people.add(b);

people.add(c);

people.set(0, d);   //.set(index, element);     //將d唐僧放到list中索引為0的位置,替換a白龍馬

people.add(1, e);   //.add(index, element);     //將e悟空放到list中索引為1的位置,原來位置的b沙和尚後移一位

//增強for迴圈遍歷list

for(String str:people){

System.out.println(str);

}