1 import java.util.Scanner; 2 public class array03{ 3 public static void main(String[] args){ 4 Scanner myScanner = new Scanner(System.in); 5 6 7 8 9 int[] arr = {1,2,3,4,5}; 10 11 12 while(true){ 13 14 15 for (int i = 0; i < arr.length; i++) { 16 System.out.print(arr[i]); 17 if (i < arr.length - 1) { 18 System.out.print(", "); 19 20 } 21 } 22 23 24 25 if (arr.length == 1){ 26 System.out.println("已经是最后一个元素了,不能再删减了"); 27 break; 28 } 29 30 31 System.out.println("请问是否需要删减数组元素:y/n"); 32 char key = myScanner.next().charAt(0); 33 if(key == 'y'){ 34 int[] arrNew = new int[arr.length-1]; 35 for(int i = 0;i< arrNew.length; i++){ 36 arrNew[i] = arr[i]; 37 } 38 arr = arrNew; 39 40 }else{ 41 break; 42 } 43 44 } 45 46 47 48 49 } 50 }
标签:arr,缩减,int,元素,System,length,数组,arrNew From: https://www.cnblogs.com/wzyxslx/p/18561272