如何复制值,而不是引用列表与LT的;整数GT;到另一个目录? [英] How to copy values, not references, of List<Integer> into another list?

查看:109
本文介绍了如何复制值,而不是引用列表与LT的;整数GT;到另一个目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也就是说,没有引用同一个对象,我需要一个列表元素的值复制到另一个列表。这些都是列表:

Namely, without referencing the same object, I need to copy values of elements of one list into another list. These are the lists:

List<Integer> listA = new ArrayList<Integer>();
List<Integer> ListB = new ArrayList<Integer>();

listA = (added some values);

listB = (do what?)...

PS。我appologize初学者的问题,但我从来没有做过这样的事情。

PS. I appologize for beginner's question, but I have never done such a thing.

推荐答案

您可以尝试给一看<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#copy%28java.util.List,%20java.util.List%29\">Collections.copy方法:

You can try and give a look at the Collections.copy method:

公共静态无效的复印件(名单DEST,
              清单SRC)

public static void copy(List dest, List src)

将所有从一个列表中的元素到另一个。在手术后,每个索引复制
  在目的地列表中元素将是相同的其在索引
  源列表。至少只要目的地列表必须是为
  源列表。如果较长,在剩余的元素
  目标列表不受影响。此方法以线性时间运行。

Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected. This method runs in linear time.

参数:目标 - 目标列表。 src - 源列表

Parameters: dest - The destination list. src - The source list.

注:以上应适用于简单的数据类型,例如整型,但是,如果你有自己的对象,这可能反过来引用其他对象,你将不得不遍历每个对象,并分别将其复制

Note: The above should work for simple data types such as Integers, however, if you have your own objects which might in turn reference other objects, you will have to iterate over each object and copy it separately.

这篇关于如何复制值,而不是引用列表与LT的;整数GT;到另一个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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