Java Collections复制列表 - 我不明白 [英] Java Collections copy list - I don't understand

查看:118
本文介绍了Java Collections复制列表 - 我不明白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayList ,我想复制它。我使用实用程序类,如果可能的话,假设有人花了一些时间使它正确。因此,我自然会得到包含复制方法的 Collections 类。

I have an ArrayList and I want to copy it exactly. I use utility classes when possible on the assumption that someone spent some time making it correct. So naturally, I end up with the Collections class which contains a copy method.

假设我有以下:

List<String> a = new ArrayList<String>();
a.add("a");
a.add("b");
a.add("c");
List<String> b = new ArrayList<String>(a.size());

Collections.copy(b,a);

这会失败,因为基本上它认为 b isn足够容纳 a 。是的,我知道 b 有0,但应该足够大现在不应该吗?如果我必须首先填充 b ,那么 Collections.copy()在我心目中成为一个完全无用的函数。所以,除了编程一个复制函数(我现在要做的)有一个正确的方法来做这个?

This fails because basically it thinks b isn't big enough to hold a. Yes I know b has size 0, but it should be big enough now shouldn't it? If I have to fill b first, then Collections.copy() becomes a completely useless function in my mind. So, except for programming a copy function (which I'm going to do now) is there a proper way to do this?

推荐答案

p>调用

List<String> b = new ArrayList<String>(a);

创建 a code> b 。所有元素将以 a (假设它有一个顺序)中完全相同的顺序存在于 b 中。

creates a shallow copy of a within b. All elements will exist within b in the exact same order that they were within a (assuming it had an order).

同样,调用

// note: instantiating with a.size() gives `b` enough capacity to hold everything
List<String> b = new ArrayList<String>(a.size());
Collections.copy(b, a);

还会创建 a b 。如果第一个参数 b 没有足够的容量(不是size)来包含 a 的元素,那么它会抛出一个 IndexOutOfBoundsException 。期望是 Collections.copy 工作,如果有,则抛出异常。这是一个优化,要求复制的集合预分配( b ),但我一般不认为这个功能是值得的,因为所需的检查给定的构造函数

also creates a shallow copy of a within b. If the first parameter, b, does not have enough capacity (not size) to contain all of a's elements, then it will throw an IndexOutOfBoundsException. The expectation is that no allocations will be required by Collections.copy to work, and if any are, then it throws that exception. It's an optimization to require the copied collection to be preallocated (b), but I generally do not think that the feature is worth it due to the required checks given the constructor-based alternatives like the one shown above that have no weird side effects.

要创建一个深度副本, List 通过任一机制,将必须具有底层类型的复杂知识。在 String s的情况下,它们在Java(和.NET)中是不可变的,你甚至不需要一个深拷贝。在 MySpecialObject 的情况下,您需要知道如何对其进行深层复制,而不是通用操作。

To create a deep copy, the List, via either mechanism, would have to have intricate knowledge of the underlying type. In the case of Strings, which are immutable in Java (and .NET for that matter), you don't even need a deep copy. In the case of MySpecialObject, you need to know how to make a deep copy of it and that is not a generic operation.

注意:最初接受的答案是Google中 Collections.copy 的最高结果,错误,如在评论中指出。

Note: The originally accepted answer was the top result for Collections.copy in Google, and it was flat out wrong as pointed out in the comments.

这篇关于Java Collections复制列表 - 我不明白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆