PoeticalJustice

导航

练习:自己写一个容器ArrayList集合 一一数组综合练习2

  1 package cn.bjsxt.collection;
  2 
  3 /**
  4  * 自己实现一个ArrayList
  5  */
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 
  9 public class SxtArrayList /* implements List */{
 10 
 11     private Object[] elementDate;
 12     // 要放入的元素大小
 13     private int size;
 14 
 15     public int size() {
 16         return size;
 17     }
 18 
 19     // 无参构造器
 20     public SxtArrayList() {
 21         // 默认容量10
 22         this(10);
 23 
 24     }
 25 
 26     // 有参构造器 initialCapacity初始容量
 27 
 28     public SxtArrayList(int initialCapacity) {
 29 
 30         if (initialCapacity < 0) {
 31             try {
 32                 throw new Exception();
 33             } catch (Exception e) {
 34 
 35                 e.printStackTrace();
 36             }
 37         }
 38         elementDate = new Object[initialCapacity];
 39     }
 40 
 41     // 添加方法
 42     public void add(Object obj) {
 43         // 数组扩容
 44         if (size >= elementDate.length) {
 45             Object[] newArray = new Object[size * 3 / 2 + 1];
 46             // 数据copy 原数组
 47             System.arraycopy(elementDate, 0, newArray, 0, elementDate.length);
 48             // for(int i=0;i<elementDate.length;i++){
 49             // newArray[i]=elementDate[i];
 50             // }
 51             // 换成新的数组
 52             elementDate = newArray;
 53         }
 54         elementDate[size] = obj;
 55         size++;
 56     }
 57 
 58     // 判断是否为空
 59     public boolean isEmpty() {
 60         return size == 0;
 61     }
 62 
 63     // 获取下标索引元素
 64     public Object get(int index) {
 65         // 索引判断的方法 我们下面对其进行了封装 既方法 rangeCheck();
 66         rangeCheck(index);
 67         // if(index<0||index>=size){
 68         // try {
 69         // throw new Exception();
 70         // } catch (Exception e) {
 71         // // TODO Auto-generated catch block
 72         // e.printStackTrace();
 73         // }
 74         // }
 75         return elementDate[index];
 76     }
 77 
 78     public void remove(int index) {
 79         // 下标判断
 80         rangeCheck(index);
 81 
 82         int numMoved = size - index - 1;
 83         if (numMoved > 0) {
 84 
 85             // 数据copy
 86             System.arraycopy(elementDate, index + 1, elementDate, index,
 87                     numMoved);
 88         }
 89         elementDate[size] = null;
 90         --size;
 91 
 92         elementDate=elementDate;
 93 
 94     }
 95     public void remove(Object obj){
 96         
 97         for(int i=0;i<size;i++){
 98             //底层掉的是equals方法 而不是==
 99             if(get(i).equals(obj)){
100                 remove(i);
101                 
102             }
103         }
104         
105     }
106     //
107     public Object set(int index,Object obj){
108         rangeCheck(index);
109         Object oldValue = elementDate[index];
110         return oldValue;
111     }
112 
113     public void rangeCheck(int index) {
114         // 索引判断的方法  我们下面对其进行了封装 既方法  rangeCheck();
115         if (index >= size) {
116             try {
117                 throw new Exception();
118             } catch (Exception e) {
119                 
120                 e.printStackTrace();
121             }
122         }
123 
124     }
125     public void ensureCapacity(){
126         // 数组扩容
127         if (size >= elementDate.length) {
128             Object[] newArray = new Object[size * 3 / 2 + 1];
129             // 数据copy 原数组
130             System.arraycopy(elementDate, 0, newArray, 0, elementDate.length);
131             // for(int i=0;i<elementDate.length;i++){
132             // newArray[i]=elementDate[i];
133             // }
134             // 换成新的数组
135             elementDate = newArray;
136         }
137     }
138     //
139     public void add(int index , Object obj){
140         //下标判断
141         rangeCheck(index);
142         // 数组扩容
143         ensureCapacity();
144         
145         System.arraycopy(elementDate, index, elementDate, index+1, size-index);
146         elementDate[index]=obj;
147         size++;
148     }
149 
150     public static void main(String[] args) {
151 
152         SxtArrayList list = new SxtArrayList(3);
153         list.add("333");
154         list.add("444");
155         list.add("333");
156         list.add("333");
157         list.add("444");
158         list.add("333");
159         // System.out.println(list.size());
160         // System.out.println(list.isEmpty());
161         // System.out.println(list.get(4));
162         // System.out.println(list.get(5));
163         // System.out.println(list.get(6));
164 //        list.remove(5);
165 //        System.out.println(list.get(5));
166         System.out.println(list.size());
167         for(int i=0;i<list.size();i++){
168             System.out.println(list.get(i));
169         }
170     }
171 }

 

posted on 2017-10-12 21:46  PoeticalJustice  阅读(302)  评论(0编辑  收藏  举报