java - Values in the array when inserted in the loop -


i'm unable figure out why myarray[i] in program below printing 0 3 times.

int[] myarray = new int[6]; (int = 0; <= 5; i++) {     myarray[i] = i++;     system.out.println("myarray:"+myarray[i]);      //need know how print object myarray[i]?      }   system.out.println("outside for" +arrays.tostring(myarray));     

my output is:

myarray:0 myarray:0 myarray:0 outside for[0, 0, 2, 0, 4, 0] 

i'm not understanding why myarray 0..

the default value of int in java zero. therefore int array initialized 0 in of it's indexes. during first iteration of loop following variable states...

myarray = [0, 0, 0, 0, 0, 0]; = 0; 

the value of 0 therefore 0 inserted myarray[0]. incremented. printing value of myarray[1] '0'. @ end of loop following state of variable...

myarray = [0, 0, 0, 0, 0, 0]; = 1; 

now value of incremented loop , value of becomes 2. inserting value of 2 myarray[2]. , value of incremented because of post increment operator. after loop executes state of variables.

myarray = [0, 0, 2, 0, 0, 0]; = 3; 

now value of incremented loop , value of becomes 4.now inserting value of 4 myarray[4]. , value of incremented because of post increment operator. after loop executes state of variables.

myarray = [0, 0, 2, 0, 4, 0]; = 5; 

now value of incremented , condition of loop broken. print value of array... output's following data...

outside for[0, 0, 2, 0, 4, 0] 

because of post increment operation value of myarray prints value of next position. default value of int 0 0 printed in output.

if remove post increment operation think you'll find trying. , following link provides how increment works in java...

link

here link on how debug application using eclipse ide...

link


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -