I am new to programming, and I just learned about array. However, in one of the quiz questions, I can't figure out why my answer is wrong. I tried to explain my thinking.
The question is:
int[] b = { 1, 2, 3 };
int[] c = b;
c[0] += b[2];
c[1] += b[1];
c[2] += b[0];
System.out.println(c[0] + c[1] + c[2]);
My thinking is although array is a reference type, int[] c = b can still be treated as
int [] c = { 1, 2, 3 }; // the same as int [] b
c[0] += b[2] means c[0] = c[0] + b[2] = 1 + 3 = 4
c[1] += b[1] means c[1] = c[1] + b[1] = 2 + 2 = 4
c[2] += b[0] means c[2] = c[2] + b[0] = 3 + 1 = 4
So I thought that the output is 12. However, the answer is 15.
I am so sorry for asking such a basic question, but I was wondering if anyone can help me understand how to come up with 15. Thank you so much!
