Java Why is the Answer 10 10 10 10 10 10 10 10 10 10 I cant
Java: Why is the Answer: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]? I can\'t understand why it\'s not [10, 10, 20, 30, ...]
What elements do the array list contain after the following code is excecuted?
int[] numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
for (int i = 1; i < 10; i++){
numbers[i] = numbers[i - 1];
}
Solution
Answer:
numbers[i] = numbers[i - 1];
20=10 // numbers[1] = numbers[0];
next time when loop run it always get number 10
you starting your loop from numbers[i] means 20
numbers[i] = numbers[i - 1]; means at position 1 assign value of previous position . so in this case 20 is replace by 10
and next time it always replace value 10 with 30 40 so on.
![Java: Why is the Answer: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]? I can\'t understand why it\'s not [10, 10, 20, 30, ...] What elements do the array list conta Java: Why is the Answer: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]? I can\'t understand why it\'s not [10, 10, 20, 30, ...] What elements do the array list conta](/WebImages/43/java-why-is-the-answer-10-10-10-10-10-10-10-10-10-10-i-cant-1136099-1761608130-0.webp)