1

I'm unable to figure out why myArray[i] in the program below is printing only 0 three times.

int[] myArray = new int[6];
for (int i = 0; i <= 5; i++) {
    myArray[i] = i++;
    System.out.println("myArray:"+myArray[i]); 
    //need to know how to print this 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 is always 0..

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
nandu
  • 21
  • 5

6 Answers6

1

The problem is you increment i in two different places. One is here:

for (int i=0;i<=5;i++) {

The other is here:

myArray[i]=i++;

Remove the ++ from the second one and it should work fine.

nhouser9
  • 6,730
  • 3
  • 21
  • 42
1

The default value of int in java is zero. Therefore your int array is initialized to zero in all of it's indexes. During the first iteration of your loop the following are your variable states...

myArray = [0, 0, 0, 0, 0, 0];
i = 0;

the value of i is 0 therefore zero is inserted into myArray[0]. Then i is incremented. now you are printing the value of myArray[1] which is '0'. At the end of the loop the following is the state of your variable...

myArray = [0, 0, 0, 0, 0, 0];
i = 1;

now the value of i is incremented by for loop and the value of i becomes 2. now you are inserting the value of 2 is myArray[2]. and then the value of i is incremented because of post increment operator. After the loop executes this is the state of your variables.

myArray = [0, 0, 2, 0, 0, 0];
i = 3;

now the value of i is incremented by for loop and the value of i becomes 4.now you are inserting the value of 4 is myArray[4]. and then the value of i is incremented because of post increment operator. After the loop executes this is the state of your variables.

myArray = [0, 0, 2, 0, 4, 0];
i = 5;

now the value of i is incremented and the condition of for loop is broken. then you print the value of the array... which output's the following data...

Outside for[0, 0, 2, 0, 4, 0]

because of the post increment operation your value of myArray always prints the value of the next position. As the default value of int is zero you always get zero printed in your output.

if you remove the post increment operation i think you'll find what you were trying. and the following link provides how increment works in java...

link

here is a link on how to debug an application using eclipse IDE...

link

Community
  • 1
  • 1
Abhishek
  • 2,485
  • 2
  • 18
  • 25
1

Well, notice that the place where you put the value of I++ is not the value that you print:

For examle, on the first iteration, I=1 So you enter into myarr[1] the value of 2. For now on, the value of I is 2, so when you want to print my arr[i], you print are in the place of 2 which is 0.

Adi Ml
  • 125
  • 2
  • 12
0

You have to assign the value with i+1, otherwise your variable doesnt save the new value and skip few steps in the loop. That's why it loops only 3 times and not 6 times, you increment i++ on two places that influents your loop. So do this:

myArray[i]=i+1; 

Or just add i to the array, it depends on you:

myArray[i]=i;

Read more about prefix and postfix operators here.

Community
  • 1
  • 1
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
0

No need to increment i twice i.e. i is incremented in the for loop as well as in first line in the loop. Also note that when you are assigning value inside the loop,value of i is assigned at ith index and then it is incremented.

int[] myArray=new int[6];
for (int i=0;i<=5;i++) {
    // myArray[i]=i++; Do not increment i here
    myArray[i]=i;
    System.out.println("Value at "+i+"th index in myArray: "+myArray[i]);    
}  
System.out.println("Outside for" +Arrays.toString(myArray)); 
Rohan Kushwaha
  • 738
  • 6
  • 18
0

This is what happens: int[] are initialized with 0

LOOP1: i = 0
Array gets assigned with 0 and i becomes 1
Then i++ increments i to 2

LOOP2: i = 2
myArray[2] = 2 (note that myArray[1] is still 0) and i becomes 3
Then i++ increment i to 4

LOOP3: i = 4
myArray[4] = 4 (myArray[3] is still 0) and i becomes 5
Then i++ increments i to 6

Loop terminates.

Final result in your array: [0, 0, 2, 0, 4, 0]

You can see other answers for the ways of fixing this issue.

Community
  • 1
  • 1
Prashant
  • 4,775
  • 3
  • 28
  • 47