E e = data[pos]; if (pos != size - 1) { for (int i = pos; i < size - 1; i++) { data[i] = data[i + 1]; } } size--; data[size] = null; if (size < data.length * limit) resize(data.length / 2);
return e; }
//O(n) 重制容器的容积 privatesynchronizedvoidresize(int capacity){ if (capacity < 5) { return; } E[] newData = (E[]) new Object[capacity]; for (int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData; }
@Override public String toString(){ StringBuilder res = new StringBuilder(); res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length)); res.append('['); for (int i = 0; i < size; i++) { res.append(data[i]); if (i != size - 1) res.append(", "); } res.append(']'); System.out.println("内存中的数据{" + Arrays.toString(data) + "}"); return res.toString(); }
publicstaticvoidmain(String[] args){ Array<Integer> array = new Array<>(); for (int i = 0; i <= 20; i++) array.put(i);
for (int i = 0; i < 15; i++) { array.delete(0); System.out.println(array + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); }