我刚做的数组的插入和删除,请高手帮忙改进:import java.util.Arrays;
import java.util.Scanner;public class ArrayAddAndRemove
{
static int[] a =
{
4, 2, 7, 3, 5
};// 初始数组
static int sum = 5;// 数组的有效位数 public static void main(String[] args)
{
System.out.println("初始数组为:");
output();
while (true)
{
System.out.println("操作:1:插入;2:删除;3:退出");

Scanner in = new Scanner(System.in);

int option = in.nextInt();
if (option == 1)
{
System.out.println("请输入插入的位置:");
int pos = in.nextInt();
System.out.println("请输入要插入的数:");
int num = in.nextInt();
add(pos, num);
System.out.println("插入后的数组为:");
output();
}
else if (option == 2)
{
System.out.println("请输入要删除的数的位置:");
int pos1 = in.nextInt();
remove(pos1);
System.out.println("删除后的数组为:");
output();
}
else
break;
}
} static void add(int pos, int num)
{ // 将num插入到数组的第pos下标中,其他元素向后移动
if (sum == a.length)
{ // 扩充数组空间
a = Arrays.copyOf(a, a.length * 2);
}
for (int i = sum; i > pos; i--)
{
a[i] = a[i - 1];
}
a[pos] = num;
sum++; // 有效位数加1
} static void remove(int pos)
{ // 删除数组中下标为pos的元素
sum--; // 有效位数减1
for (int i = pos; i < sum; i++)
{
a[i] = a[i + 1];
}
} static void output()
{ // 遍历输出数组中的元素
for (int i = 0; i < sum; i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
}
}