Java: how do I initialize an array size if it's unknown?

前端 未结 7 633
庸人自扰
庸人自扰 2021-02-04 03:47

I\'m asking the user to enter some numbers between 1 and 100 and assign them into an array. The array size is not initialized since it is dependent on the number of times the us

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-04 04:00

    int i,largest = 0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the number of numbers in the list");
    i = scan.nextInt();
    int arr[] = new int[i];
    System.out.println("Enter the list of numbers:");
    for(int j=0;j

    The above code works well. I have taken the input of the number of elements in the list and initialized the array accordingly.

提交回复
热议问题