|
| 1 | +package 基本数据结构; |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * Created by LIANG on 2017/2/24. |
| 6 | + */ |
| 7 | +public class MyArrayList<E> implements MyList<E> |
| 8 | +{ |
| 9 | + public static final int INITIAL_CAPACITTY = 16; |
| 10 | + private E[] data = (E[]) new Object[INITIAL_CAPACITTY]; |
| 11 | + private static int size = 0; |
| 12 | + |
| 13 | + public MyArrayList(E[] objects) |
| 14 | + { |
| 15 | + |
| 16 | + for (int i = 0; i < objects.length; i++) |
| 17 | + { |
| 18 | + add(objects[i]); |
| 19 | + size++; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public MyArrayList() {} |
| 24 | + |
| 25 | + ; |
| 26 | + |
| 27 | + public void ensureCapicity() |
| 28 | + { |
| 29 | + if (size >= data.length) |
| 30 | + { |
| 31 | + E[] newData = (E[]) new Object[size * 2]; |
| 32 | + System.arraycopy(data, 0, newData, 0, data.length); |
| 33 | + data = newData; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | +// public boolean add(E e) |
| 38 | +// { |
| 39 | +// ensureCapicity(); |
| 40 | +// if(e != null) |
| 41 | +// data[size++] = e; |
| 42 | +// return true; |
| 43 | +// } |
| 44 | + |
| 45 | + public void add(E e) |
| 46 | + { |
| 47 | + add(size, e); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void add(int index, E e) |
| 52 | + { |
| 53 | + ensureCapicity(); |
| 54 | + for (int i = size - 1; i >= index; i--) |
| 55 | + { |
| 56 | + data[i + 1] = data[i]; |
| 57 | + } |
| 58 | + data[index] = e; |
| 59 | + size++; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public E get(int index) |
| 64 | + { |
| 65 | + return data[index]; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public E remove(int index) |
| 70 | + { |
| 71 | + checkIndex(index); |
| 72 | + E e = data[index]; |
| 73 | + for (int i = index; i < data.length - 1; i++) |
| 74 | + data[i] = data[i + 1]; |
| 75 | + |
| 76 | + data[size - 1] = null; |
| 77 | + size--; |
| 78 | + return e; |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + private void checkIndex(int index) |
| 83 | + { |
| 84 | + if (index < 0 || index >= size) |
| 85 | + throw new IndexOutOfBoundsException("输入下标有误"); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public int size() |
| 90 | + { |
| 91 | + return size; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void clear() |
| 96 | + { |
| 97 | + data = (E[]) new Object[INITIAL_CAPACITTY]; |
| 98 | + size = 0; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public int indexOf(E e) |
| 103 | + { |
| 104 | + for (int i = 0; i < size; i++) |
| 105 | + { |
| 106 | + if (e.equals(data[i])) |
| 107 | + return i; |
| 108 | + } |
| 109 | + return -1; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + @Override |
| 114 | + public boolean isEmpty() |
| 115 | + { |
| 116 | + return size == 0; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public E set(int index, E e) |
| 121 | + { |
| 122 | + checkIndex(index); |
| 123 | + E temp = data[index]; |
| 124 | + data[index] = e; |
| 125 | + return temp; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean contains(E e) |
| 130 | + { |
| 131 | + for (int i = 0; i < size; i++) |
| 132 | + { |
| 133 | + if(data[i].equals(e)) |
| 134 | + return true; |
| 135 | + } |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + public void trimToSize() |
| 140 | + { |
| 141 | + if (size != data.length) |
| 142 | + { |
| 143 | + E[] newData = (E[]) new Object[size]; |
| 144 | + System.arraycopy(data, 0, newData, 0, size); |
| 145 | + data = newData; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public String toString() |
| 151 | + { |
| 152 | + StringBuilder result = new StringBuilder("["); |
| 153 | + |
| 154 | + for (int i = 0; i < size; i++) |
| 155 | + { |
| 156 | + result.append(data[i]); |
| 157 | + if (i < size - 1) |
| 158 | + result.append(","); |
| 159 | + } |
| 160 | + return result.toString() + "]"; |
| 161 | + } |
| 162 | + |
| 163 | + private class ArrayListIterator implements Iterator |
| 164 | + { |
| 165 | + private int current = 0; |
| 166 | + |
| 167 | + @Override |
| 168 | + public boolean hasNext() |
| 169 | + { |
| 170 | + return (current < size); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public E next() |
| 175 | + { |
| 176 | + return data[current++]; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void remove() |
| 181 | + { |
| 182 | + MyArrayList.this.remove(current); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments