Why all elements of my list seems to be the same?

前端 未结 2 680
忘了有多久
忘了有多久 2021-01-22 09:53

I have the following code:

Integer[] lastExchange = new Integer[nColors];
Integer[] newExchange = new Integer[nColors];
while (true) {
    ...
    for (int i=0;          


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 10:15

    What is the type of lastExchange? If it's a reference to an object, the the problem is probably exactly that; you just add the same reference to a mutable object, which is then modified and added again.

    Since the first loop prints the object before it (presumably) is modified, it prints the proper (different) values.

提交回复
热议问题