-4

I was wondering, does array.length give you the number of elements in the array, or the size of the space inside the array that you declared. For example if I declare the following array:

int [] Array = new Array[10];

and then add one element to the array, will array.length be 1 or 10?

Also is there any way to find the other number. For example if array.length corresponds to the number of elements in the array, is there any way to find the space inside of the array?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
albad17
  • 1
  • 1
  • 1
    Try it and find out :) – en_Knight Feb 24 '15 at 02:32
  • 3
    There is no such thing as "the number of elements in the array" being different from its length; the array will start out with 10 zeroes, all of which are "elements" of the array. – Louis Wasserman Feb 24 '15 at 02:33
  • [Perhaps this can enlighten you?](http://stackoverflow.com/questions/23730092/how-can-i-get-the-size-of-an-array-a-collection-or-a-string-in-java) (I do go over the nuance of it a bit there.) – Makoto Feb 24 '15 at 02:35
  • Java arrays are zero indexed, the addressable range of the array will be [0-9], just so you are aware – MadProgrammer Feb 24 '15 at 02:36
  • 1
    You can't "add elements to arrays". Arrays have a fixed size which can never be changed. – user253751 Feb 24 '15 at 02:37

2 Answers2

4
  1. The length of the array is its capacity.
amahfouz
  • 2,328
  • 2
  • 16
  • 21
-1

It's 10. Length returns capacity of array regardless of it's elements.

wakwakwak99
  • 321
  • 1
  • 2
  • 9