Java中ArrayList使用范例代码
import java.util.ArrayList; public class myArrayList { public static void main(String[] args) { ArrayList<Integer> vals = new ArrayList<Integer>(); vals.add(100); vals.add(90); vals.add(80); vals.add(70); vals.add(60); int total = 0; /*for(int i = 0; i < vals.size(); ++i) { total += vals.get(i); }*/ for (Integer val : vals) { total += val; } double average = total / vals.size(); System.out.println("Size of vals: " + vals.size()); System.out.println("Average: " + average); vals.remove(4); System.out.println("New size: " + vals.size()); vals.add(60); vals.add(50); System.out.println("Newer size: " + vals.size()); } }