Kyle Reese
Kyle Reese

Reputation: 9

Why is the output for this code in this way?

class MainDemo{ 

     public static void main(String args[]){ 
        float arrayOne[] = {1, 2, 3, 4,5}; 
        for(int iIndex=0; iIndex<arrayOne.length; iIndex++) 
        { 
               arrayOne[iIndex] = iIndex; 
               iIndex=iIndex+1; 
         } 
         for(int iIndex=0; iIndex<arrayOne.length; iIndex++) 
         { 
               System.out.println(arrayOne[iIndex]); 
         } 
 } 
}


Why is the output?

0.0
2.0
2.0
4.0
4.0

instead of

0.0
1.0
2.0
3.0
4.0

Upvotes: 0

Views: 148

Answers (7)

Achintya Jha
Achintya Jha

Reputation: 12843

Beacuse of this line iIndex=iIndex+1; .

for(int iIndex=0; iIndex<arrayOne.length; iIndex++) //iIndex get incremented by 1 here.
    { 
           arrayOne[iIndex] = iIndex; 
           iIndex=iIndex+1; //iIndex get incremented by 1 here.
     } 

The above loop will start from 0 and get incremented by 2 each time as mentioned above. So iIndex values will be 0,2,4... .Value at index 1,3... remains unchanged and values at 0,2,4... get replaced by iIndex value.

Upvotes: 0

subodh
subodh

Reputation: 6158

public static void main(String args[]){ 
            float arrayOne[] = {1, 2, 3, 4,5}; 
            for(int iIndex=0; iIndex<arrayOne.length; iIndex++) 
            { 
                   arrayOne[iIndex] = iIndex; 
                  // iIndex=iIndex+1; comment this line, twice increment is not required. 
             } 
             for(int iIndex=0; iIndex<arrayOne.length; iIndex++) 
             { 
                   System.out.println(arrayOne[iIndex]); 
             } 
     }

Upvotes: 0

sumit sharma
sumit sharma

Reputation: 1057

Dude because when you start your first loop then it change the value of index 0,2 and 4 whose previous values are 1,3 and 5 respectively and for these index new value after loop are 0,2 and 4 respectively thats why you get the output

0.0
2.0
2.0
4.0
4.0

instead of

1.0
2.0
3.0
4.0
5.0

Upvotes: 2

hsuk
hsuk

Reputation: 6860

Because increment has been done twice:

 for(int iIndex=0; iIndex<arrayOne.length; iIndex++)  
 /* Forloop itself increments iIndex*/

and

 iIndex=iIndex+1; 
 /*You are manually incrementing iIndex*/

Upvotes: 2

Jace
Jace

Reputation: 3072

In your first loop:

for(int iIndex=0; iIndex<arrayOne.length; iIndex++) //<-- here
{ 
      arrayOne[iIndex] = iIndex; 
      iIndex=iIndex+1; //<-- here - get rid of this
} 

You add to iIndex twice. I've made notes above.

Get rid of the second one as you already increment iIndex as part of your for loop definition.

Upvotes: 1

radai
radai

Reputation: 24192

because youre only replacing indexes 0 2 and 4 in the original array and leaving 1 and 3 untouched (iIndex is incremented by 2 each loop iteration)

Upvotes: 3

Ajinkya
Ajinkya

Reputation: 22710

Because you are updating only 0,2 and 4th index of array. As iIndex is updated twice once in loop in once if for statement
Original array

float arrayOne[] = {1, 2, 3, 4,5};  

Updated array

   float arrayOne[] = {0, 2, 2, 4,4};
                       |_____|____|______ Are updated 

Remove

iIndex=iIndex+1;  

If you want to update every value.

Upvotes: 1

Related Questions