|
| 1 | +/** |
| 2 | + * |
| 3 | + * 1)泛型动态数组 |
| 4 | + * |
| 5 | + * Author: shi |
| 6 | + */ |
| 7 | + |
| 8 | + public class GenericArray<T> { |
| 9 | + private T[] data; |
| 10 | + private int size; |
| 11 | + |
| 12 | + // 根据传入容量,构造Array |
| 13 | + public GenericArray(int capacity) { |
| 14 | + data = (T[]) new Object[capacity]; |
| 15 | + size = 0; |
| 16 | + } |
| 17 | + |
| 18 | + // 无参构造方法,默认数组容量为10 |
| 19 | + public GenericArray() { |
| 20 | + this(10); |
| 21 | + } |
| 22 | + |
| 23 | + // 获取数组容量 |
| 24 | + public int getCapacity() { |
| 25 | + return data.length; |
| 26 | + } |
| 27 | + |
| 28 | + // 获取当前元素个数 |
| 29 | + public int count() { |
| 30 | + return size; |
| 31 | + } |
| 32 | + |
| 33 | + // 判断数组是否为空 |
| 34 | + public boolean isEmpty() { |
| 35 | + return size == 0; |
| 36 | + } |
| 37 | + |
| 38 | + // 修改 index 位置的元素 |
| 39 | + public void set(int index, T e) { |
| 40 | + checkIndex(index); |
| 41 | + data[index] = e; |
| 42 | + } |
| 43 | + |
| 44 | + // 获取对应 index 位置的元素 |
| 45 | + public T get(int index) { |
| 46 | + checkIndex(index); |
| 47 | + return data[index]; |
| 48 | + } |
| 49 | + |
| 50 | + // 查看数组是否包含元素e |
| 51 | + public boolean contains(T e) { |
| 52 | + for (int i = 0; i < size; i++) { |
| 53 | + if (data[i].equals(e)) { |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + // 获取对应元素的下标, 未找到,返回 -1 |
| 61 | + public int find(T e) { |
| 62 | + for ( int i = 0; i < size; i++) { |
| 63 | + if (data[i].equals(e)) { |
| 64 | + return i; |
| 65 | + } |
| 66 | + } |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + // 在 index 位置,插入元素e, 时间复杂度 O(m+n) |
| 72 | + public void add(int index, T e) { |
| 73 | + checkIndex(index); |
| 74 | + // 如果当前元素个数等于数组容量,则将数组扩容为原来的2倍 |
| 75 | + if (size == data.length) { |
| 76 | + resize(2 * data.length); |
| 77 | + } |
| 78 | + |
| 79 | + for (int i = size - 1; i >= index; i--) { |
| 80 | + data[i + 1] = data[i]; |
| 81 | + } |
| 82 | + data[index] = e; |
| 83 | + size++; |
| 84 | + } |
| 85 | + |
| 86 | + // 向数组头插入元素 |
| 87 | + public void addFirst(T e) { |
| 88 | + add(0, e); |
| 89 | + } |
| 90 | + |
| 91 | + // 向数组尾插入元素 |
| 92 | + public void addLast(T e) { |
| 93 | + add(size, e); |
| 94 | + } |
| 95 | + |
| 96 | + // 删除 index 位置的元素,并返回 |
| 97 | + public T remove(int index) { |
| 98 | + checkIndex(index); |
| 99 | + |
| 100 | + T ret = data[index]; |
| 101 | + for (int i = index + 1; i < size; i++) { |
| 102 | + data[i - 1] = data[i]; |
| 103 | + } |
| 104 | + size --; |
| 105 | + data[size] = null; |
| 106 | + |
| 107 | + // 缩容 |
| 108 | + if (size == data.length / 4 && data.length / 2 != 0) { |
| 109 | + resize(data.length / 2); |
| 110 | + } |
| 111 | + |
| 112 | + return ret; |
| 113 | + } |
| 114 | + |
| 115 | + // 删除第一个元素 |
| 116 | + public T removeFirst() { |
| 117 | + return remove(0); |
| 118 | + } |
| 119 | + |
| 120 | + // 删除末尾元素 |
| 121 | + public T removeLast() { |
| 122 | + return remove(size - 1); |
| 123 | + } |
| 124 | + |
| 125 | + // 从数组中删除指定元素 |
| 126 | + public void removeElement(T e) { |
| 127 | + int index = find(e); |
| 128 | + if (index != -1) { |
| 129 | + remove(index); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public String toString() { |
| 135 | + StringBuilder builder = new StringBuilder(); |
| 136 | + builder.append(String.format("Array size = %d, capacity = %d \n", size, data.length)); |
| 137 | + builder.append('['); |
| 138 | + for (int i = 0; i < size; i++) { |
| 139 | + builder.append(data[i]); |
| 140 | + if (i != size - 1) { |
| 141 | + builder.append(", "); |
| 142 | + } |
| 143 | + } |
| 144 | + builder.append(']'); |
| 145 | + return builder.toString(); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + // 扩容方法,时间复杂度 O(n) |
| 150 | + private void resize(int capacity) { |
| 151 | + T[] newData = (T[]) new Object[capacity]; |
| 152 | + |
| 153 | + for (int i = 0; i < size; i++) { |
| 154 | + newData[i] = data[i]; |
| 155 | + } |
| 156 | + data = newData; |
| 157 | + } |
| 158 | + |
| 159 | + private void checkIndex(int index) { |
| 160 | + if (index < 0 || index > size) { |
| 161 | + throw new IllegalArgumentException("Add failed! Require index >=0 and index <= size."); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
0 commit comments