List集合-ArrayList

实现思路:

1.初始化一个Object[] 的数据,在构造函数中默认空的数组,在add()方法中new出空间。初始size为10.

2.(核心)底层利用Arrays.copyOf()方法进行扩容处理,每次扩展容量的一半。

3.指定添加位置时,利用System.arraycopy()方法将之后的元素统一向后移动一位,最后在插入。

git上项目路径: https://github.com/0ziyu0/handWriting

代码:

接口

 1 public interface CustomerList<E> {
 2     
 3     public void add(E object);
 4     
 5     public void add(int index, E object);
 6     
 7     public Object get(int index);
 8     
 9     public Object remove(int index);
10     
11     public Boolean remove(E object);
12     
13     public int size();
14 }

实现

  1 public class CustomerArrayList<E> implements CustomerList<E> {
  2     
  3     private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
  4     
  5     private static final Object[] EMPTY_ELEMENTDATA = {};
  6     
  7     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
  8     
  9     private transient Object[] elementData;
 10     
 11     private int size;
 12     
 13     private static final int DEFAULT_CAPACITY = 10;
 14     
 15     public CustomerArrayList() {
 16         // 默认是空数组
 17 //        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
 18         this.elementData = new Object[DEFAULT_CAPACITY];
 19     }
 20     
 21     public CustomerArrayList(int initialCapacity) {
 22         
 23         if(initialCapacity > 0) {
 24             this.elementData = new Object[initialCapacity];
 25         } else if(initialCapacity == 0) {
 26             this.elementData = EMPTY_ELEMENTDATA;
 27         } else {
 28             throw new IllegalArgumentException("数组参数初始化错误...");
 29         }
 30         
 31     }
 32     
 33     // 进行扩容处理
 34     private void ensureCapacityInternal(int minCapacity) {
 35         
 36         if(size == elementData.length) {
 37             int newCapacity = size + size >> 1;// 扩容一半
 38             if(newCapacity < minCapacity) { // 防止初始化容器为1的情况下,上述位移运算后扩容大小与原大小一样
 39                 newCapacity = minCapacity;
 40             }
 41             elementData = Arrays.copyOf(elementData, newCapacity);
 42         }
 43         
 44     }
 45     
 46     public void checkBorder(int index) {
 47         if(index >= size || index < 0) {
 48             throw new RuntimeException("数组越界...");
 49         }
 50     }
 51 
 52     @Override
 53     public void add(E object) {
 54         
 55         ensureCapacityInternal(size + 1);
 56         elementData[size++] = object;
 57         
 58     }
 59 
 60     @Override
 61     public void add(int index, E object) {
 62         
 63         checkBorder(index);
 64         ensureCapacityInternal(size + 1);
 65         // 将index后面的值全部向后移动一位,然后插入新值
 66         int removeIndex = size - index; 
 67         if(removeIndex > 0) {
 68             System.arraycopy(elementData, index, elementData, index + 1, removeIndex);
 69         }
 70         elementData[index] = object;
 71         size++;
 72     }
 73 
 74     @Override
 75     public Object get(int index) {
 76         
 77         checkBorder(index);
 78         
 79         return elementData[index];
 80     }
 81 
 82     @Override
 83     public Object remove(int index) {
 84         
 85         checkBorder(index);
 86         // 将index后面的值向前移动一位,将最后一位置为null,size减1
 87         Object removeObj = elementData[index];
 88         int removeIndex = elementData.length - index - 1;
 89         if(removeIndex > 0) {
 90             System.arraycopy(elementData, index + 1, elementData, index, removeIndex);
 91         }
 92         elementData[--size] = null;
 93         return removeObj;
 94     }
 95 
 96     @Override
 97     public Boolean remove(E object) {
 98         
 99         try {
100             for(int i = 0; i < size; i++) {
101                 if(object.equals(elementData[i])) {
102                     remove(i);
103                 }
104             }
105         } catch (Exception e) {
106             e.printStackTrace();
107             throw new RuntimeException();
108         }
109         
110         return true;
111     }
112 
113     @Override
114     public int size() {
115         
116         return size;
117     }
118     
119     
120 }

测试代码

 1 public class ListTest {
 2 
 3     @Test
 4     public void testArrayListAdd001() {
 5         
 6         CustomerList<String> list = new CustomerLinkedList<String>();
 7         list.add("test1");
 8         list.add("test2");
 9         list.add("test3");
10         
11         print(list);
12         list.remove(1);
13         print(list);
14     }
15     
16     @Test
17     public void testArrayListAdd002() {
18         
19         CustomerList<String> list = new CustomerLinkedList<String>();
20         list.add("test1");
21         list.add("test2");
22         list.add("test3");
23         
24         print(list);
25         
26         list.add(0, "test0");
27         print(list);
28         
29     }
30     
31     @Test
32     public void testRemove001() {
33         
34         CustomerList<String> list = new CustomerLinkedList<String>();
35         list.add("test1");
36         list.add("test2");
37         list.add("test3");
38         
39         print(list);
40         
41         list.remove(1);
42         print(list);
43         
44     }
45     
46     @Test
47     public void testRemove002() {
48         
49         CustomerList<String> list = new CustomerLinkedList<String>();
50         list.add("test1");
51         list.add("test2");
52         list.add("test3");
53         
54         print(list);
55         
56         list.remove("test2");
57         print(list);
58         
59     }
60     
61     @Test
62     public void testSize001() {
63         
64         CustomerList<String> list = new CustomerLinkedList<String>();
65         list.add("test1");
66         list.add("test2");
67         list.add("test3");
68         
69         System.out.println(list.size());
70         
71     }
72     
73     private void print(CustomerList<String> list) {
74         System.out.println("=======");
75         for(int i = 0; i < list.size(); i++) {
76             System.out.println(list.get(i));
77         }
78     }
79     
80 }

运行截图

 

 

 

 

posted @ 2019-01-16 21:01  东隅已逝x  Views(183)  Comments(0)    收藏  举报