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

前端 未结 7 638
庸人自扰
庸人自扰 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:01

    I think you need use List or classes based on that.

    For instance,

    ArrayList integers = new ArrayList();
    int j;
    
    do{
        integers.add(int.nextInt());
        j++;
    }while( (integers.get(j-1) >= 1) || (integers.get(j-1) <= 100) );
    

    You could read this article for getting more information about how to use that.

提交回复
热议问题