返回顶部

收藏

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());
   }
}

标签:ArrayList,数组列表,Java

收藏

0人收藏

支持

0

反对

0

发表评论