This is the code. I tried to solve it, but I can\'t understand how its output is 111111?
public class Test {
public static void main(String[] args) {
Hi I will try to explain what is happening in this code snippet
public class Test {
public static void main(String[] args) {
int list[] = {1, 2, 3, 4, 5, 6};
This line is the equivalent of the following code
int list[] = new int[6];
list[0] = 1;
list[1] = 2;
list[2] = 3;
list[3] = 4;
list[4] = 5;
list[5] = 6;
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
this loop is the equivalent of the following code
list[1] = list[0];
list[2] = list[1];
list[3] = list[2];
list[4] = list[3];
list[5] = list[4];
which because list[0] = 1
is the equivalent of the following
list[1] = 1;
list[2] = 1;
list[3] = 1;
list[4] = 1;
list[5] = 1;
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}
Which is why you are getting the output you are getting
The output is 1 1 1 1 1 1
because the first for
statement is looping over an array, named list
, of values (i.e. {1, 2, 3, 4, 5, 6}
) and replacing each one with the previous one, starting at the 2nd one (index 1
; or, as some may say, the 1st one where the "real" first one is the 0th one … I digress). Thus the 2nd (index 1
) item's current value (2
) gets replaced by the 1st (index 0
) item's value, which is 1
: (list[i] = list[i - 1];
where i = 1
). This continues down the array of integers until we reach the end of list
. As we replace each value with the one before it, they all eventually just become the same value – that of the first item in the array, which is 1
. In the end we have this value for our list
: {1, 1, 1, 1, 1, 1}
.
The next for
statement simply prints out each value of the updated list
in turn followed by a blank space: System.out.print(list[i] + " ");
. So, as you can see, the result of running this code is that it prints out 1 1 1 1 1 1
.
Just to drive the point home in a more visual way, let's just map out the values of the list
array over time:
int i = 1; // i = 1
list[i] = list[i - 1]; // list[1] = list[1 - 1]; list[0] == 1
// list is now {1, 1, 3, 4, 5, 6}
i++; // i = i + 1 # i is now 2
list[i] = list[i - 1]; // list[2] = list[2 - 1]; list[1] == 1
// list is now {1, 1, 1, 4, 5, 6}
i++; // i = i + 1; i = 3
list[i] = list[i - 1]; // list[3] = list[3 - 1]; list[2] == 1
// list is now {1, 1, 1, 1, 5, 6}
i++; // i = i + 1; i = 4
list[i] = list[i - 1]; // list[4] = list[4 - 1]; list[3] == 1
// list is now {1, 1, 1, 1, 1, 6}
i++; // i = i + 1; i = 5
list[i] = list[i - 1]; // list[5] = list[5 - 1]; list[4] == 1
// list is now {1, 1, 1, 1, 1, 1}
i++; // i = i + 1; i = 6
i < list.length; // i == 6; list.length == 6; 6 < 6 == false
// exit the for loop
I sure hope that helps you understand the concepts at play here a little better. Good luck on the rest of your test!
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
is equivalent to:
list[1] = list[1-1] = list[0] = 1
list[2] = list[2-1] = list[1] = 1
list[3] = list[3-1] = list[2] = 1
list[4] = list[4-1] = list[3] = 1
list[5] = list[5-1] = list[4] = 1
Got it?