import java.util.Scanner; public class ArrayShrinking { public static void main(String[] args){ Scanner myScanner = new Scanner(System.in); int[] arr = {1,2,3,4,5}; System.out.println("=======未缩减数组为======"); for(int i = 0;i<arr.length;i++){ System.out.print(arr[i] + "\t"); } do{ int[] arrNew = new int[arr.length - 1]; if(arrNew.length == 0){ System.out.println("\n该数组已缩减到最小空间了,不能继续缩减了"); return; } for(int i = 0;i<arrNew.length;i++){ arrNew[i] = arr[i]; } arr = arrNew; System.out.println("\n=======缩减完数组为======"); 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); } }
02-24
2173
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)