如果我在 Java 中返回一个 List,返回值是引用还是实际值?

发布于 2024-10-18 11:59:12 字数 93 浏览 1 评论 0原文

非常不言自明:我需要修改我收到的 ArrayList,但我希望原始的保持不变。

我知道这是非常基本的,我不知道为什么我不知道明确的答案。我想迟到总比不到好。

Pretty self explanatory: I need to modify the ArrayList that I receive, but I want the original to be unchanged.

I know this is very basic, and I'm not sure why I don't know the definitive answer. I guess better late than never though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

高速公鹿 2024-10-25 11:59:12

您将获得对原始ArrayList的引用。

您可以使用 克隆()

如果您有以下问题,请查看这个问题想要深拷贝。

You will have a reference to the original ArrayList.

You can create a shallow copy of the list with clone().

Have a look at this question if you want a deep copy.

辞别 2024-10-25 11:59:12

java中的所有内容默认都是引用。所以是的,更改返回的数组列表将修改原始数组列表。

为了防止这一问题,您必须创建原始副本的副本。使用 .clone() 方法。

Everything in java will be a reference by default. So yes changing the returned arraylist will modify the original one.

To prevent that problem you have to create a copy of the original one. Use the .clone() method for that.

瑶笙 2024-10-25 11:59:12

如果您想要修改列表,但不修改原始列表,则不应对在方法参数中收到的列表进行操作,因为您对引用进行操作。最好使用这样的东西:

public void modifyList(List myList) {
    myList.add("aaa"); // original *will* be modified
    List modifiable = new ArrayList(myList);
    modifiable.add("bbb"); // original will *not* be modified - only the copy
}

If you want a modified list, but not to modify the original, you shouldn't operate on the list which you received in arguments of the method because you operate on reference. Better use something like this:

public void modifyList(List myList) {
    myList.add("aaa"); // original *will* be modified
    List modifiable = new ArrayList(myList);
    modifiable.add("bbb"); // original will *not* be modified - only the copy
}
守望孤独 2024-10-25 11:59:12

我认为java始终是按值传递的。

唯一要记住的是,对象是通过传递其地址值或对它们的引用来传递的,这使得它们看起来像是在 C++ 术语中通过引用传递的。因为本质上,您传递的是引用地址而不是对象的副本。地址按值传递。

因此,Java 可以说它总是按值传递,因为即使它传递一个对象,它也只是传递其堆堆栈上的内存引用的值,而不是目标对象的克隆/副本。

Arraylist 是一个对象,因此它将按值传递,因此传入的值将是对堆上原始 Arraylist 的引用。对函数中 Arraylist 的任何修改都会修改堆上的原始数组。

我没有使用过.clone()。我把这个留给其他人来帮忙。同时我也会亲自去了解一下。

I think java is pass-by-value always.

The only thing to remember is that objects are passed by passing the value of their address, or reference to them, this makes it seem as if they are passed by reference in C++ terms. Because essentially, you are passing the reference address not a copy of the object. The address is passed by value.

Thus, Java can say it is always passed by value because even when it is passing an object, it is only passing the value of its memory reference on the heap stack, not a clone/copy of the target object.

An Arraylist is an object, therefore it will be passed by value, so the value passed in will be a reference to the original Arraylist on the heap. Any modification to the Arraylist in your function, will modify the original on the heap.

I have not used the .clone(). I leave that to others to help with. Meanwhile, I will go learn about them myself.

恰似旧人归 2024-10-25 11:59:12

它将是相同的 ArrayList。如果您想要副本,则必须自己复制。如果 ArrayList 包含复杂的对象,则不一定容易!

It will be the same ArrayList. If you want a copy, you'll have to copy it yourself. Not necessarily easy if the ArrayList holds complex objects!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文