|
| 1 | +public class Array<E> { |
| 2 | + |
| 3 | + private E[] data; |
| 4 | + private int size; |
| 5 | + |
| 6 | + /** |
| 7 | + * 构造函数,传入数组的容量 capacity 构造 Array |
| 8 | + * |
| 9 | + * @param capacity |
| 10 | + */ |
| 11 | + public Array(int capacity) { |
| 12 | + data = (E[]) new Object[capacity]; // new E[capacity] 不支持该语法,历史遗留问题 |
| 13 | + size = 0; |
| 14 | + } |
| 15 | + |
| 16 | + // 无参数的构造函数,默认数组的容量 capacity=10 |
| 17 | + public Array() { |
| 18 | + this(10); |
| 19 | + } |
| 20 | + |
| 21 | + // 获取数组中的元素个数 |
| 22 | + public int getSize() { |
| 23 | + return size; |
| 24 | + } |
| 25 | + |
| 26 | + // 获取数组的容量 |
| 27 | + public int getCapacity() { |
| 28 | + return data.length; |
| 29 | + } |
| 30 | + |
| 31 | + // 返回数组是否为空 |
| 32 | + public boolean isEmpty() { |
| 33 | + return size == 0; |
| 34 | + } |
| 35 | + |
| 36 | + // 向所有元素后添加一个新元素 |
| 37 | + public void addLast(E e) { |
| 38 | + /*if (size == data.length) { |
| 39 | + throw new IllegalArgumentException("AddLast failed. Array is full."); |
| 40 | + } |
| 41 | +
|
| 42 | + data[size] = e; |
| 43 | + size++;*/ |
| 44 | + add(size, e); |
| 45 | + } |
| 46 | + |
| 47 | + // 在所有元素前添加一个新元素 |
| 48 | + public void addFirst(E e) { |
| 49 | + add(0, e); |
| 50 | + } |
| 51 | + |
| 52 | + // 在第 index 个位置插入一个新元素 e |
| 53 | + public void add(int index, E e) { |
| 54 | + |
| 55 | + if (index < 0 || index > size) { |
| 56 | + throw new IllegalArgumentException("add failed. Require index >=0 and index < size."); |
| 57 | + } |
| 58 | + if (size == data.length) { |
| 59 | + resize(2 * data.length); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + for (int i = size - 1; i >= index; i--) { |
| 64 | + data[i + 1] = data[i]; |
| 65 | + } |
| 66 | + data[index] = e; |
| 67 | + size++; |
| 68 | + } |
| 69 | + |
| 70 | + //获取 index 索引位置的元素 |
| 71 | + public E get(int index) { |
| 72 | + if (index < 0 || index >= size) { |
| 73 | + throw new IllegalArgumentException("Get failed. Index is illegal."); |
| 74 | + } |
| 75 | + return data[index]; |
| 76 | + } |
| 77 | + |
| 78 | + public E getLast() { |
| 79 | + return get(size - 1); |
| 80 | + } |
| 81 | + |
| 82 | + public E getFirst() { |
| 83 | + return get(0); |
| 84 | + } |
| 85 | + |
| 86 | + // 修改 index 索引位置的元素 |
| 87 | + public void set(int index, E e) { |
| 88 | + if (index < 0 || index >= size) { |
| 89 | + throw new IllegalArgumentException("Set failed. Index is illegal."); |
| 90 | + } |
| 91 | + data[index] = e; |
| 92 | + } |
| 93 | + |
| 94 | + // 查找数组中是否有元素 e |
| 95 | + public boolean contains(E e) { |
| 96 | + for (int i = 0; i < size; i++) { |
| 97 | + if (data[i].equals(e)) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // 查找数组中元素 e 所在的索引,如果不存在元素 e,则返回 -1 |
| 105 | + public int find(E e) { |
| 106 | + for (int i = 0; i < size; i++) { |
| 107 | + if (data[i].equals(e)) { |
| 108 | + return i; |
| 109 | + } |
| 110 | + } |
| 111 | + return -1; |
| 112 | + } |
| 113 | + |
| 114 | + // 从数组中删除 index 位置的元素,返回删除的元素 |
| 115 | + public E remove(int index) { |
| 116 | + |
| 117 | + if (index < 0 || index > size) { |
| 118 | + throw new IllegalArgumentException("remove failed. Index is illegal."); |
| 119 | + } |
| 120 | + E ret = data[index]; |
| 121 | + for (int i = index + 1; i < size; i++) { |
| 122 | + data[i - 1] = data[i]; |
| 123 | + } |
| 124 | + size--; |
| 125 | + |
| 126 | + // 该行可选 |
| 127 | + data[size] = null; // loitering objects != memory leak |
| 128 | + |
| 129 | + if (size == data.length / 4 && data.length / 2 != 0) { |
| 130 | + resize(data.length / 2); |
| 131 | + } |
| 132 | + |
| 133 | + return ret; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + // 从数组中删除第一个元素,返回删除的元素 |
| 138 | + public E removeFirst(int index) { |
| 139 | + |
| 140 | + return remove(0); |
| 141 | + } |
| 142 | + |
| 143 | + // 从数组中删除最后元素,返回删除的元素 |
| 144 | + public E removeLast() { |
| 145 | + |
| 146 | + return remove(size - 1); |
| 147 | + } |
| 148 | + |
| 149 | + // 从数组中删除元素e |
| 150 | + public void removeElement(E e) { |
| 151 | + int index = find(e); |
| 152 | + if (index != -1) { |
| 153 | + remove(index); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + @Override |
| 159 | + public String toString() { |
| 160 | + StringBuilder res = new StringBuilder(); |
| 161 | + res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length)); |
| 162 | + res.append("["); |
| 163 | + for (int i = 0; i < size; i++) { |
| 164 | + res.append(data[i]); |
| 165 | + if (i != size - 1) { |
| 166 | + res.append(", "); |
| 167 | + } |
| 168 | + } |
| 169 | + res.append("]"); |
| 170 | + return res.toString(); |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + private void resize(int newCapacity) { |
| 175 | + E[] newData = (E[]) new Object[newCapacity]; |
| 176 | + for (int i = 0; i < size; i++) { |
| 177 | + newData[i] = data[i]; |
| 178 | + } |
| 179 | + data = newData; |
| 180 | + } |
| 181 | +} |
0 commit comments