简体   繁体   English

从同一个数组创建两个列表,修改一个列表,更改另一个列表

[英]Created two Lists from same array, Modifying one List, changes the other

I created two list from same array and sorted one of them. 我从同一个数组创建了两个列表并对其中一个进行了排序。 When I tried to change one list, other list also got updated. 当我尝试更改一个列表时,其他列表也已更新。

List<Integer> list = Arrays.asList(ar);
List<Integer> sorted = Arrays.asList(ar);
Collections.sort(sorted);
list.set(0,10000000); //changes sorted also

It took me a while figure out, below mentioned code worked. 我花了一段时间才弄明白,下面提到的代码工作了。

List<Integer> sorted = new ArrayList<Integer>(Arrays.asList(ar));

I want to know why my first approach didn't work? 我想知道为什么我的第一种方法不起作用? I created two separate lists, why the changes are taking place in both of them. 我创建了两个单独的列表,为什么这两个列表都发生了变化。 How does java assign values to variables here? java如何在这里为变量赋值?

From the Java documentation for Arrays.asList : Arrays.asList的Java文档:

Returns a fixed-size list backed by the specified array. 返回由指定数组支持的固定大小的列表。 (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray() . (对返回列表的更改“直写”到数组。)此方法与Collection.toArray()结合,充当基于数组的API和基于集合的API之间的桥梁。 The returned list is serializable and implements RandomAccess . 返回的列表是可序列化的并实现RandomAccess

So when you change something in list, it "writes through" to the underlying array, ar , which is also the underlying array in sorted, so the change is reflected in sorted as well. 因此,当您更改列表中的某些内容时,它会“写入”到底层数组ar ,这也是已排序的基础数组,因此更改也会反映在已排序中。

Also, the code for asList is: 此外, asList的代码是:

public static <T> List<T> asList(T... a) {
    return new ArrayList<T>(a);
}

This is java.util.Arrays.ArrayList , which has the following definition: 这是java.util.Arrays.ArrayList ,它具有以下定义:

ArrayList(E[] array) {
    a = Objects.requireNonNull(array);
}

What is important is that a is not copied, it is the original array. 重要的是a不复制,它是原始数组。 The java.util.ArrayList class has the following constructor java.util.ArrayList类具有以下构造函数

public ArrayList(Collection<? extends E> c) {
     elementData = c.toArray();
     size = elementData.length;
     // c.toArray might (incorrectly) not return Object[] (see 6260652)
     if (elementData.getClass() != Object[].class)
         elementData = Arrays.copyOf(elementData, size, Object[].class);
 }

so in the java.util.ArrayList constructor, we create copies of each element, and in java.util.Arrays.ArrayList , we do not. 所以在java.util.ArrayList构造函数中,我们创建了每个元素的副本,而在java.util.Arrays.ArrayList ,我们没有。

Arrays有自己的ArrayList实现,它不会从toList复制数组

A List is a collection of objects, and both list are collections of the same objects. List是对象的集合,两个列表都是相同对象的集合。 The set statement changes an object, and the object is shared by both lists. set语句更改了一个对象,该对象由两个列表共享。

I don't understand why the second version works. 我不明白为什么第二个版本有效。

listsorted仍然指向第一种方法中的ar的相同内存地址,而在第二种方法中,在调用构造函数之后已经分配了新的内存地址,新的内存块被分配给类对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从同一个pojo数组创建了两个列表,修改了一个列表,同样的事情也会影响其他列表 - Created two Lists from same pojo array, Modifying one List, same thing affects on other list also Kotlin 检查两个列表是否相同,除了一个具有附加元素的列表? - Kotlin check if two lists are the same apart from one list having an additional element? 如何将链表分成两个其他列表,一个具有奇数索引,另一个具有偶数索引但以递归方式 - How to segregate a linked list into two other lists, one with odd indices the other one with even indices but in recursion way Java Streams - 从其他两个列表中获取“对称差异列表” - Java Streams - Get a "symmetric difference list" from two other lists 如何通过比较两个列表从一个列表中删除元素 - How to remove element from one list by comparing two lists 如何创建合并两个其他列表的列表? - How to create a list that merges two other lists? 活动中的两个对象,修改一个会自动修改另一个 - Two objects in activity, modifying one automatically modifies the other 如何从字符串数组中仅获取一项,如果另一项相同 - how to get just one item from array of strings if the other is the same 将两个子列表混合成一个列表 - Mix two sub-lists into one List Java 从其他两个具有不同对象和共同属性的列表构建一个列表 - Java build a list from two other lists with different objects and common property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM