1.插入:
public static void main(String[] args) throws Exception {
int[] arr = new int[] { 1, 2, 3, 4 };
simulateInsert(arr, 2, 9);
}
private static void simulateInsert(int[] arr, int i, int j)
throws Exception {
int[] b = null;
if (i > arr.length) {
throw new Exception("越界");
}
if (i < 1 || i > arr.length - 1) {
throw new Exception("不在范围内");
}
// 不在表尾
if (i != arr.length - 1) {
b = Arrays.copyOf(arr, arr.length + 1);
// 递减的目的是先存储住之前的假如正着,会把之前的通通覆盖
for (int k = b.length - 1; k >= i - 1; k--) {
b[k] = b[k - 1];
}
b[i - 1] = j;
}
}
2.删除:
public static void main(String[] args) throws Exception {
int[] arr = new int[] { 1, 2, 3, 4 };
Simula