将一个列表添加到java中的另一个列表?

编程入门 行业动态 更新时间:2024-10-21 11:44:10
本文介绍了将一个列表添加到java中的另一个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下java代码。

I have below java code.

List<SomePojo> list = new ArrayList<SomePojo>(); //add 100 SomePojo objects to list.

现在列表有100个对象。

Now list has 100 objects.

如果我再创建一个实例,如下所示:

If i create one more instance as below:

List<SomePojo> anotherList = new ArrayList<SomePojo>(); anotherList .addAll(list);

谢谢!

推荐答案

对象在内存中只有一次。您首次添加列表只会添加对象引用。

An object is only once in memory. Your first addition to list just adds the object references.

anotherList.addAll 也将只添加引用。所以内存中只有100个对象。

anotherList.addAll will also just add the references. So still only 100 objects in memory.

如果通过添加/删除元素更改 list , anotherList 不会被更改。但是如果你更改 list 中的任何对象,那么当从 anotherList 访问它时,它的内容也会被更改,因为两个清单都指出了相同的参考。

If you change list by adding/removing elements, anotherList won't be changed. But if you change any object in list, then it's content will be also changed, when accessing it from anotherList, because the same reference is being pointed to from both lists.

更多推荐

将一个列表添加到java中的另一个列表?

本文发布于:2023-11-23 15:57:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!