Is java array with an element null is of length 1? -


below code:

int[][][] @ = { {{9, 4}, null},  {null},  {{1, 2, 3}} }; system.out.println(at.length + " " + at[1].length);  // 3 1 

shows at[1].length 1.

is not that, length counted when have element of type int[][] @ second index in object pointed at? if yes, why at[1] null of length 1? because null not of type int[][]

similar example:

mytype[] t = {null}; system.out.println(t.length); //1 

for me t.length should 1, when array has element {new mytype()} or element instance of subtype of mytype, like,

mytype[] t = {new mytype()}; system.out.println(t.length); //1 

yes.

in fact, when declare , initialize array of object references, values inside null default. remember length of array never vary, despite contents (null elements) in array. there's difference between length , size, length how many elements can stored in array, while size amount of valid elements in array. also, array is not list.

example:

string[] stringarray = { null, null }; system.out.println(stringarray.length); stringarray[0] = "hello"; stringarray[1] = "world"; system.out.println(stringarray.length); 

prints:

2 2 

from jls(emphasis mine):

an array object contains number of variables. number of variables may zero, in case array said empty. (...) if array has n components, n length of array; components of array referenced using integer indices 0 n - 1, inclusive

(...)

once array object created, its length never changes.

(...)

an array created array creation expression (§15.10.1) or array initializer (§10.6)

an array creation expression specifies element type, number of levels of nested arrays, , length of array @ least 1 of levels of nesting. the array's length available final instance variable length.

an array initializer creates array , provides initial values components.

(...)

the length of array constructed equal number of variable initializers enclosed braces of array initializer. space allocated new array of length. if there insufficient space allocate array, evaluation of array initializer completes abruptly throwing outofmemoryerror. otherwise, a one-dimensional array created of specified length, , each component of array initialized default value.

(...)

  • the public final field length, contains number of components of array. length may positive or zero.

from here, may understand following:

  • when initialize array, have define @ least length of first level (a.k.a. dimension). if have array of single level, length length of whole array.
  • when array initialized, components (a.k.a. elements) initialized default value of type. primitive types, may 0 or false (depends on type). object references, it's null.
  • the length of array represents how many components can have.
  • an array has fixed length. length cannot changed.

again, length of array (how many components can store) is not same size of array (how many components consider proper values storing @ moment).


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -