Array Length in Java

前端 未结 16 1474
忘了有多久
忘了有多久 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:05

    `

    int array[]=new int[3]; array.length;

    so here we have created an array with a memory space of 3... this is how it looks actually

    0th 1st 2nd ...........> Index 2 4 5 ...........> Number

    So as u see the size of this array is 3 but the index of array is only up to 2 since any array starts with 0th index.

    second statement' output shall be 3 since the length of the array is 3... Please don't get confused between the index value and the length of the array....

    cheers!

提交回复
热议问题