手动实现ArrayList容器

  1 package text;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 public class SxtArrayList{
  7     private Object[] elementData;
  8     private int size;
  9     
 10     public int size(){
 11         return size;
 12     }
 13     public boolean isEmpty(){
 14         return size ==0;
 15     }
 16     public SxtArrayList(){
 17         this(10);
 18     }
 19     public SxtArrayList(int initialCapacity){    //构造器
 20         if(initialCapacity<0){
 21             try{
 22                 throw new Exception();
 23             }catch (Exception e){
 24                 e.printStackTrace();
 25     }
 26     }
 27     elementData= new Object[initialCapacity];
 28     
 29     }
 30     
 31     public void add(Object obj){
 32         //数组扩容
 33         if(size==elementData.length){
 34             Object[] newArray= new Object[size*2+1];
 35 //            System.arraycopy(elementData,0,newArray,0,elementData.length);
 36             for(int i=0;i<elementData.length;i++){
 37                 newArray[i]=elementData[1];
 38             }
 39             elementData= newArray;
 40         }
 41         elementData[size]=obj;
 42         size++;
 43     }
 44     public Object get(int index){
 45         if(index<0||index>=size){
 46             try{
 47                 throw new Exception();
 48             }catch (Exception e){
 49                 e.printStackTrace();
 50             }
 51         }
 52         return elementData[index];
 53     }
 54     public void remove(int index){
 55         if(index<0||index>=size){
 56             try{
 57                 throw new Exception();
 58             }catch (Exception e){
 59                 e.printStackTrace();
 60             }
 61             
 62         }
 63         int numMoved=size-index-1;
 64         if(numMoved>0){
 65             System.arraycopy(elementData,index+1,elementData,index,numMoved);
 66         }
 67         elementData[--size]=null;
 68     }
 69     public void rangeCheck(int index){
 70         if(index<0||index>=size){
 71             try{
 72                 throw new Exception();
 73             }catch (Exception e){
 74                 e.printStackTrace();
 75             }
 76             
 77         }
 78     }
 79 //    public void remove(Object obj){
 80 //        for(int i=0;i<size;i++){
 81 //            if(get(i).equals(obj)){//注意:底层调用的equals方法,不是==
 82 //                remove(i);
 83 //            }
 84 //        }
 85 //    }
 86     public Object set(int index,Object obj){
 87         rangeCheck(index);
 88         Object oldValue= elementData[index];
 89         elementData[index]=obj;
 90         return oldValue;
 91     }
 92     public static void main(String[] args){
 93         SxtArrayList list2=new SxtArrayList(3);
 94         list2.add("222");
 95         list2.add("222");
 96         list2.add("222");
 97         list2.add("222");
 98         list2.add("222");
 99         list2.add("222");
100         System.out.println(list2.size);
101         
102         Object str2=list2.get(5);
103         System.out.println(str2);
104         list2.remove(2);
105         System.out.println(list2.size);
106     }
107     
108 }

猜你喜欢

转载自www.cnblogs.com/qingsheng/p/9118562.html