简体   繁体   English

有关引用ArrayList元素的简单Java问题

[英]Simple Java question about references to ArrayList element

Say I have the following code, 说我有以下代码,

LinkedList partials = new LinkedList();
partials.add(new ArrayList());
ArrayList head = partials.element();
head.add("Test");

I want "head" to simply be a copy of the Arraylist that is a result of partials.element(). 我希望“ head”只是成为由partials.element()生成的Arraylist的副本。 However, now when I make changes to "head", it is reflected in the Arraylist partials. 但是,现在当我对“ head”进行更改时,它会反映在Arraylist部分中。 How do I make a copy of the Arraylist that is the first element of partials such that making changes to the Arraylist won't be reflected in partials? 我如何制作Arraylist的副本,它是partials的第一个元素,这样对Arraylist所做的更改将不会反映在partials中?

Two options come to mind: 我想到两个选择:

  • Call the ArrayList constructor which takes another collection - that'll make a copy 调用需要另一个集合的ArrayList构造函数-它将创建一个副本
  • Call clone() on the ArrayList ArrayList上调用clone()

Note that both of these will create shallow copies - each list will contain references to the same objects. 请注意,这两个都将创建浅表副本-每个列表将包含对相同对象的引用。 That's fine if they're immutable (like strings) but if you want to create a deeper copy you'll need to do more work. 如果它们是不可变的(例如字符串),那很好,但是如果您想创建更深的副本,则需要做更多的工作。

Is there any reason you're not using generics, by the way? 顺便说一句,您是否有任何理由不使用泛型? For example, I would probably use: 例如,我可能会使用:

LinkedList<ArrayList<String>> partials = new LinkedList<ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();
list.add("Test");

// Create a shallow copy and add a reference to that into the linked list
partials.add(new ArrayList<String>(list));

list.add("Another element");

// Prints 1, because the lists are distinct
System.out.println(partials.element().size());

If nothing should ever change the contents of the lists in the linked list, you may want to look at the immutable collections available in Guava , or wrap your clone using Collections.unmodifiableList . 如果没有什么改变链接列表中列表的内容,则可能需要查看Guava中可用的不可变集合,或使用Collections.unmodifiableList包装您的克隆。 Note that the latter only creates a view on the original list, so you'd still need to perform the clone step first. 请注意,后者仅在原始列表上创建一个视图 ,因此您仍然需要首先执行克隆步骤。

actually the code can't compile at line 3 : partials.element() returns an Object (the first element of your LinkedList) and you have to cast it as an ArrayList if you want to use it this way. 实际上,代码无法在第3行进行编译:partials.element()返回一个Object(您的LinkedList的第一个元素),如果要使用它,则必须将其转换为ArrayList。

I agree you don't need generics to make the code easier to add. 我同意您不需要泛型即可使代码更易于添加。 You could also define partials and head as List instead of LinkedList & ArrayList. 您也可以将局部变量定义为List而不是LinkedList和ArrayList。 Use interface instead of implementations is a good practice and avoid you to use too much specific method like element() one when you don't need to. 使用接口代替实现是一种很好的做法,避免在不需要时使用太多特定的方法,例如element()。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM