java - Array in Array, what should be tracing of this code? -
java - Array in Array, what should be tracing of this code? -
int[] ar=new int[5]; int[] numoccurence = new int[ar.length]; (int = 0; < ar.length; i++){ ++numoccurence[ar[i]]; system.out.println(numoccurence[ar[i]]); }
first of there no array in array.
-if mean line,
int[] numoccurence = new int[ar.length];
then nil passing length of ar[]
create new array
-if mean line, ++numoccurence[ar[i]];
or numoccurence[ar[i]]
. 1 time again incrementing values in numoccurence[]
, providing index of values based on values in arr[]
, (arr[i]
returns int
)
an array of array this:-
integer[] array1 = new integer[]; integer[] array2 = new integer[]; integer[][] arrays = { array1, array2};
here arrays
holds references arrays, integer reference arrays.
and said in comments code nil print 1,2,3,4,5.
and asked how code works, since not assigning values in array ar
or numoccurence
, values in them initialized 0. , hence in each iteration of loop this
++numoccurence[ar[i]]; system.out.println( numoccurence[ar[i]]);
now since values in ar
0 in code,ar[i]
@ index homecoming 0. code can replaced this
++numoccurence[0]; system.out.println(numoccurence[0]);
and since incrementing same element ++numoccurence[0];
on each iteration print incrementing values 0+1,0+1+1,.... 5 .
java arrays
Comments
Post a Comment