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];
}
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}
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
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
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] + " ");
}
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.
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);
}