|
| 1 | +package com.coding.basic; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author Hipple |
| 7 | + * @Time:2017年2月20日 下午8:53:31 |
| 8 | + * @version 1.0 |
| 9 | + */ |
| 10 | +public class ArrayList implements List { |
| 11 | + |
| 12 | + //元素数量 |
| 13 | + private int size = 0; |
| 14 | + |
| 15 | + //默认容量 |
| 16 | + private final int defaultCapacity = 10; |
| 17 | + |
| 18 | + //存储元素的容器 |
| 19 | + private static Object[] elementData; |
| 20 | + |
| 21 | + //无参构造器 |
| 22 | + public ArrayList(){ |
| 23 | + elementData = new Object[defaultCapacity]; |
| 24 | + } |
| 25 | + |
| 26 | + //指定容量的构造器 |
| 27 | + public ArrayList(int capacity){ |
| 28 | + if (capacity < 0) { |
| 29 | + //非法参数 |
| 30 | + throw new IllegalArgumentException("Illegal Capacity: "+ capacity); |
| 31 | + } |
| 32 | + elementData = new Object[capacity]; |
| 33 | + } |
| 34 | + |
| 35 | + //添加元素 |
| 36 | + public boolean add(Object o){ |
| 37 | + ensureCapacityInternal(size + 1); |
| 38 | + elementData[size++] = o; |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + //添加元素到指定位置 |
| 43 | + public void add(int index, Object o){ |
| 44 | + rangeCheck(index); |
| 45 | + //将当前位置及后续元素后移一位 |
| 46 | + ensureCapacityInternal(size + 1); |
| 47 | + System.arraycopy(elementData, index, elementData, index+1, size-index); |
| 48 | + elementData[index] = o; |
| 49 | + size++; |
| 50 | + } |
| 51 | + |
| 52 | + //根据下表获取值 |
| 53 | + public Object get(int index){ |
| 54 | + rangeCheck(index); |
| 55 | + return elementData[index]; |
| 56 | + } |
| 57 | + |
| 58 | + //删除元素 |
| 59 | + public Object remove(int index){ |
| 60 | + rangeCheck(index); |
| 61 | + Object oldValue = elementData[index]; |
| 62 | + int numMoved = size - index - 1; |
| 63 | + if (numMoved > 0) { |
| 64 | + //要删除的元素不是最后一个时,将当前元素及后续元素左移一位 |
| 65 | + System.arraycopy(elementData, index+1, elementData, index, numMoved); |
| 66 | + } |
| 67 | + elementData[--size] = null;//自动回收 |
| 68 | + return oldValue; |
| 69 | + } |
| 70 | + |
| 71 | + //删除元素 |
| 72 | + public boolean remove(Object o) { |
| 73 | + // 由于ArrayList中允许存放null,因此下面通过两种情况来分别处理。 |
| 74 | + if (o == null) { |
| 75 | + for (int index = 0; index < size; index++){ |
| 76 | + if (elementData[index] == null) { |
| 77 | + fastRemove(index); |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + } else { |
| 82 | + for (int index = 0; index < size; index++){ |
| 83 | + if (o.equals(elementData[index])) { |
| 84 | + fastRemove(index); |
| 85 | + return true; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + //返回现有元素数量 |
| 93 | + public int size(){ |
| 94 | + return size; |
| 95 | + } |
| 96 | + |
| 97 | + //是否为空 |
| 98 | + public boolean isEmpty(){ |
| 99 | + return size == 0; |
| 100 | + } |
| 101 | + |
| 102 | + public Iterator iterator(){ |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + //动态增加ArrayList大小 |
| 107 | + private void ensureCapacityInternal(int minCapacity) { |
| 108 | + //当前数组无法再存放时将数组长度增加至原长度的1.5倍 |
| 109 | + if (minCapacity - elementData.length > 0) { |
| 110 | + int newCapacity = (elementData.length * 3)/2; |
| 111 | + elementData = Arrays.copyOf(elementData, newCapacity); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + //检查是否下标越界 |
| 117 | + private void rangeCheck(int index){ |
| 118 | + if (index < 0 || index > this.size) { |
| 119 | + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + //删除元素,与remove的 差别就是没有下标检查 |
| 124 | + private void fastRemove(int index) { |
| 125 | + int numMoved = size - index - 1; |
| 126 | + if (numMoved > 0){ |
| 127 | + System.arraycopy(elementData, index + 1, elementData, index, numMoved); |
| 128 | + } |
| 129 | + elementData[--size] = null; |
| 130 | + } |
| 131 | + |
| 132 | +} |
| 133 | +class testArrayList{ |
| 134 | + public static void main(String[] args) { |
| 135 | + ArrayList arrayList = new ArrayList(); |
| 136 | + for (int i = 0; i < 10; i++) { |
| 137 | + arrayList.add(i+1); |
| 138 | + } |
| 139 | + arrayList.add(5,15); |
| 140 | + arrayList.remove(11); |
| 141 | + for (int i = 0; i < arrayList.size(); i++) { |
| 142 | + System.out.println("value is "+arrayList.get(i)); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments