可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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) {
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}
回答1:
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?
回答2:
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!
回答3:
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
This part of your code causes value of all items in list is 1.
When i = 1, list[1] = list[0] = 1;
when i = 2, list[2] = list[1] = 1; //not 2 because it is already overrided when i = 1.
.... to last one.
P/S: Try to understand step by step in your code before asking question.
回答4:
It's all because of this loop,
for (int i = 1; i < list.length; i++)
list[i] = list[i - 1];
What happened is, your first value is 1
, As per the loop It assigns that value to the corresponding second element of the list
Array.
That means if your list is supposed to have 10 as first element of array like this
int list[] = {10, 2, 3, 4, 5, 6};
It will print 10,10,10,10,10,10
回答5:
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
回答6:
If you're new to programming, print debugging can be a really great way to learn what is going on in your program. I've put in a bunch of print statements on your program, try running it and see if you can understand the results:
public class Test {
public static void main(String[] args) {
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++) {
System.out.println("\n\n\nloop number " + i);
System.out.println("\ni = " + i);
System.out.println("\nThe value at list[i] = " + list[i]);
System.out.println("\ni - 1 = " + (i - 1));
System.out.println("\nSo I'm accessing element " + (i - 1) +
" in the list for this iteration.");
System.out.println("\nThe value at list[i - 1] = " + list[i - 1]);
System.out.println("\nEnd of loop " + i);
list[i] = list[i - 1];
}
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}
I generally wouldn't use that many for a simple loop, but I wanted to show you different ways of accessing the values as the loop was running.
You will get something like this for each iteration of the loop in your console, it walks you through what the values are:
loop number 1
i = 1
The value at list[i] = 2
i - 1 = 0
So I'm accessing element 0 in the list for this iteration.
The value at list[i - 1] = 1
End of loop 1
回答7:
take a look at my answer here :
Why am I not getting the output I am supposed to be?
anyway... the answer is simple...
in first loop, print your array and you will see what happens to your array
something like this:
for (int i = 1; i < list.length; i++){
list[i] = list[i - 1];
System.out(list);
}
回答8:
As someone posted, your for-loop causes this result (11111). You also mentioned your question be like "It's storing values in to an array?". To my best understanding on your question, it's like you are trying to get the values from an array and store them into an another array. If so, this could be one of the options.
public static void main(String []args){
int[] list = {1, 2, 3, 4, 5, 6};
int[] myList = new int[list.length];
for (int i=0; i<list.length; i++){
myList[i] = list[i];
}
for (int i = 0; i < list.length; i++)
System.out.print(myList[i] + " ");
}
回答9:
int list[] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < list.length; i++) {
list[i] = list[i - 1];
}
When i is 1, you assign list[1] = list[0]
, here list[0] value is 1
Then array is updated {1, 1, 3, 4, 5, 6}
, loop again
When i is 2, you assign list[2] = list[1]
, here list[1] value is 1
Then array is updated {1, 1, 1, 4, 5, 6}
, loop again
In that way, array is override {1, 1, 1, 1, 1, 1}