class ArrayAdd02{
public static void main(String[] args){
//动态添加
Scanner myScanner = new Scanner(System.in);
int[] arr = {1, 2, 3};
do{
int[] arrNew = new int[arr.length + 1];//定义新的数组
for(int i = 0; i < arr.length; i++){
arrNew[i] = arr[i];
}
System.out.println("请输入数字:");
int num = myScanner.nextInt();
arrNew[arrNew.length - 1] = num;
arr = arrNew;//arr指向arrNew,arr原来的堆空间被释放,arr重新指向arrNew的地址
System.out.println("=====arr扩容后=====");
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + "\t");
}
System.out.println("添加成功,是否继续 y/n ");
char key = myScanner.next().charAt(0);
if(key == 'n'){
break;
}
}while(true);
System.out.println("程序结束...");
}
}