1 import java.util.Scanner; 2 public class array04{ 3 public static void main(String[] args){ 4 Scanner myScanner = new Scanner(System.in); 5 int[] arr = {1,2,3}; 6 do{ 7 int[] arrNew = new int[arr.length + 1]; 8 for(int i = 0;i <arr.length;i++){ 9 arrNew[i] = arr[i]; 10 } 11 12 System.out.println("输入你要添加的元素"); 13 int addNum = myScanner.nextInt(); 14 arrNew[arrNew.length - 1] = addNum; 15 16 arr = arrNew; 17 System.out.println("===arr扩容后元素情况==="); 18 for(int i = 0;i < arr.length;i++){ 19 System.out.print(arr[i] + "\t"); 20 } 21 22 System.out.println("是否继续添加 y/n"); 23 char key = myScanner.next().charAt(0); 24 if(key == 'n'){ 25 break; 26 } 27 }while(true); 28 29 30 31 32 33 34 35 36 } 37 }
标签:arr,Scanner,int,元素,数组,增加 From: https://www.cnblogs.com/wzyxslx/p/18561271