Array Length in Java

前端 未结 16 1473
忘了有多久
忘了有多久 2020-11-30 21:27

I declared an array as shown below:

int[] arr = new int[10];

Then I assigned following values to the array:

arr[0] = 1;
arr         


        
16条回答
  •  野性不改
    2020-11-30 22:21

    Java arrays are actually fixed in size, and the other answers explain how .length isn't really doing what you'd expect. I'd just like to add that given your question what you might want to be using is an ArrayList, that is an array that can grow and shrink:

    https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

    Here the .size() method will show you the number of elements in your list, and you can grow this as you add things.

提交回复
热议问题