考虑以下情况。
List<Integer> listOne = new ArrayList<>();
List<Integer> listTwo = new ArrayList<>();
listOne.add(1);I think this happens due to
listOne.add(2);
listOne.add(3);
Collections.reverse(listOne);
listTwo = listOne; //listTwo has same reference
Collections.reverse(listOne);
System.out.println(listOne); //out put [1, 2, 3]
System.out.println(listTwo); // same out put
Java is pass by value, where values (for non primitive types) happen to be references.
我认为这为这种情况下的 java 提供了生存。老实说,为什么要java
尽量避免pass by reference
和尝试不同some of other languages
?而java仍然遭受引用传递行为的困扰?
编辑:另外请有人解释上面代码中发生的事情