java - Array Initialization: int versus other Objects -
i'm confused on whether need array initialization...
for code:
private int[][][] rpos = new int[size][size][2];
can start using array right way, following line?
getlocationonscreen(rpos[i][j]); // pass array of 2 integers
and, code:
view[][] allviews = new view[size][size];
i have make nested loop, , initialize every view
calling constructors so:
for (int = 0; < size; i++) { (int j = 0; j < size; j++) { allviews[i][j] = new view(ctor1, ctor2); } }
my question is, why didn't need integer array? , also, did "new" keyword do, when typed view[][] allviews = new view[size][size];
?
why didn't need integer array?
whenever create array, array elements assigned default value component type of array. int
, default value 0
, int[]
, elements initialized 0
default.
with reference type, however, default value null
. so, issue arrays that, might potential nullpointerexception
, when try access property or method in array elements. basically, array of reference, mean array elements nothing references actual objects. don't point object.
so, access property or method, have initialize each array elements instance, avoid npe
.
what did "new" keyword do, when typed
view[][] allviews = new view[size][size];
?
it created array of array, of type view
. dimension being size x size
. since view
not primitive type, reference type. values default null
, explained.
getlocationonscreen(rpos[i][j]); // pass array of 2 integers
of course passed array of 2 integers. component type of rpos[i][j]
int[]
. default value null
too. in case, wouldn't null
, have given dimension of inner array too.
if change array declaration to:
private int[][][] rpos = new int[size][size][]; // missing last dimension
then value of rpos[i][j]
null
.
Comments
Post a Comment