【java API基本实现】ArrayList

ArrayList:

 1 package com.tn.arraylist;
 2 
 3 public class ArrayList {
 4     
 5     Object[] objects=new Object[10];
 6     int index=0;
 7     
 8     public void add(Object o){
 9         if(index==objects.length){
10             Object[] newobjects=new Object[objects.length*2];
11             System.arraycopy(objects, 0, newobjects, 0, index);
12             objects=newobjects;
13         }
14         objects[index++]=o;
15     }
16     
17     public int size(){
18         return index;
19     }
20     
21 }

客户端测试类Test:

 1 package com.tn.arraylist;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         ArrayList al=new ArrayList();
 6         for(int i=0;i<34;i++){
 7             al.add(new Object());
 8         }
 9         System.out.println(al.size());
10     }
11 }
原文地址:https://www.cnblogs.com/xiongjiawei/p/6820755.html