数组中的添加删除操作
1 public class Szcz { 2 private Object[] objs = new Object[1]; 3 private int size; 4 5 public void add(Object o) { 6 if (size == objs.length) { 7 Object[] nobjs = Arrays.copyOf(objs, objs.length + 1); 8 nobjs[size] = o; 9 objs = nobjs; 10 } else { 11 objs[size] = o; 12 } 13 size++; 14 } 15 16 public void remove(int index) { 17 Object[] o1 = new Object[index - 1]; 18 Object[] o2 = new Object[size - index - 1]; 19 for (int i = 0; i < o1.length; i++) { 20 o1[i] = objs[i]; 21 } 22 for (int i = 0; i < o2.length; i++) { 23 o2[i] = objs[index + 1 + i]; 24 } 25 int c = 0; 26 Object[] o3 = new Object[o1.length + o2.length]; 27 for (int i = 0; i < o1.length; i++) { 28 o3[i] = o1[i]; 29 c++; 30 } 31 for (int i = 0; i < o2.length; i++) { 32 o3[c] = o2[i]; 33 c++; 34 } 35 } 36 37 @Override 38 public String toString() { 39 return "Szcz [objs=" + Arrays.toString(objs) + ", size=" + size + "]"; 40 } 41 42 }