如果我更改 run() 中的 dataList,这些更改是否会反映到在其他地方声明的原始列表中?
是的。您的构造函数接收对列表的引用,而不是它的副本。如果要复制它,则必须使用LinkedList
复制构造函数。然后,您将拥有自己的列表副本。但请注意,两个列表上的条目仍然是共享的,因为条目是数组 ( short[]
),并且数组是通过引用存储的。
这也许是最好的例子:
import java.util.*;
public class ListExample {
public static final void main(String[] args) {
List<short[]> list;
// Direct use (no copies)
list = new LinkedList<short[]>();
list.add(new short[] { 0, 0, 0 });
System.out.println("list.size() before direct use: " + list.size());
System.out.println("list.get(0)[0] before direct use: " + list.get(0)[0]);
new DirectUser(list).doSomething();
System.out.println("list.size() after direct use: " + list.size());
System.out.println("list.get(0)[0] after direct use: " + list.get(0)[0]);
// Output, note how both the list and its contents have been changed:
// list.size() before direct use: 1
// list.get(0)[0] before direct use: 0
// list.size() after direct use: 2
// list.get(0)[0] after direct use: 1
// Copying the list, but note that the entries are shared by both lists:
list = new LinkedList<short[]>();
list.add(new short[] { 0, 0, 0 });
System.out.println("list.size() before copy-list use: " + list.size());
System.out.println("list.get(0)[0] before copy-list use: " + list.get(0)[0]);
new CopyListUser(list).doSomething();
System.out.println("list.size() after copy-list use: " + list.size());
System.out.println("list.get(0)[0] after copy-list use: " + list.get(0)[0]);
// Output, note how our list didn't change (it doesn't have a new entry), but
// the entry at index 0 *was* changed:
// list.size() before copy-list use: 1
// list.get(0)[0] before copy-list use: 0
// list.size() after copy-list use: 1
// list.get(0)[0] after copy-list use: 1
// "Deep" copying, both the list and its entries:
list = new LinkedList<short[]>();
list.add(new short[] { 0, 0, 0 });
System.out.println("list.size() before deep-copy use: " + list.size());
System.out.println("list.get(0)[0] before deep-copy use: " + list.get(0)[0]);
new DeepCopyUser(list).doSomething();
System.out.println("list.size() after deep-copy use: " + list.size());
System.out.println("list.get(0)[0] after deep-copy use: " + list.get(0)[0]);
// Output, note that neither the list nor its entries was affected by the call:
// list.size() before deep-copy use: 1
// list.get(0)[0] before deep-copy use: 0
// list.size() after deep-copy use: 1
// list.get(0)[0] after deep-copy use: 0
System.exit(0);
}
static class DirectUser {
List<short[]> items;
DirectUser(List<short[]> items) {
// DirectUser doesn't copy the list
this.items = items;
}
void doSomething() {
this.items.get(0)[0] = 1;
this.items.add(new short[] { 2, 2, 2 });
}
}
static class CopyListUser {
List<short[]> items;
CopyListUser(List<short[]> items) {
// CopyListUser copies the list, but both lists still share items
this.items = new LinkedList<short[]>(items);
}
void doSomething() {
this.items.get(0)[0] = 1;
this.items.add(new short[] { 2, 2, 2 });
}
}
static class DeepCopyUser {
List<short[]> items;
DeepCopyUser(List<short[]> items) {
// DeepCopyUser copies the list AND each entry
this.items = new LinkedList<short[]>();
for (short[] entry : items) {
this.items.add(Arrays.copyOf(entry, entry.length));
}
}
void doSomething() {
this.items.get(0)[0] = 1;
this.items.add(new short[] { 2, 2, 2 });
}
}
}
当DirectUser
使用列表时,在我们的调用代码中,我们看到列表(因为它变长了)和它的内容(第一个条目的第一个插槽从 更改0
为1
)都发生了变化。
使用CopyListUser
它时,它会复制列表,因此我们在调用代码中没有看到列表有任何变化(它没有变长)。但是我们确实看到了第一个条目的变化(因为两个列表共享同一个数组对象)——第一个插槽从0
变为1
再次。
使用DeepCopyUser
它时,它会复制列表和每个条目的副本,因此事情完全完全断开。我们的调用代码没有看到列表或其项目的任何更改。