java:传递引用数组?

发布于 2024-10-07 23:51:17 字数 136 浏览 4 评论 0原文

我有对象集合数组,我将此引用传递给方法,该方法将执行一些业务逻辑并更新对象,因此它将反映在原始对象中,因为我将其作为引用传递。现在我的问题是如何避免原始对象的更新。

如果涉及克隆,请让我知道我需要为上述案例进行哪种克隆

,谢谢

I am having array of collection of objects and i am passing this reference to method and the method will do some biz logic and updates the objects so it will reflect in original object since i m passing it as reference . Now my question is how to avoid this update of original object.

If cloning is involved please let me know what kind of clone i need to do for the above case

Thx

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

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

发布评论

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

评论(4

紅太極 2024-10-14 23:51:17

你的情况有点棘手:

你有一个对象的引用数组。

Java 总是按值传递,因此当您将数组的引用传递到方法中时,对数组的引用是按值传递的。

因此该方法有一个对与调用范围相同的数组的新引用。

您问题的答案是:如果您不希望更改反映在对象中,则需要复制对象。所以,要经历它。

  1. 您有一个对象引用数组。
  2. 您想要基于对象执行一些工作,但不想修改对象。
  3. 您应该创建一个对象数组——该数组中的对象是原始数组的副本。
  4. 我所说的“复制”并不是指仅执行对象复制=原始。您将需要创建一个新对象,它是副本。在相关类上使用 getCopy() 方法来创建实例的副本可能是个好主意。如果您的对象有子对象或包含其他对象,这可能会很复杂。
  5. 您将副本数组传递到执行该操作的方法中。

Your situation is a bit tricky:

You have an array of references to objects.

Java is always pass by value, so when you pass the reference to the array into the method, the reference to the array is passed by value.

So the method has a new reference to the same array as the calling scope.

The answer to your question is: if you don't want your changes to reflect in the objects, you need to copy the objects. So, to go thru it.

  1. You have an array of object references.
  2. You want to do some work based off the objects, but you don't want to modify the objects.
  3. You should create an array of objects -- the objects in this array are copies of the original array.
  4. By 'copy' I do NOT mean just do Object copy = original. You will need to create a new Object which is the copy. It might be a good idea to have a getCopy() method on the relevant class that creates copies of an instance. This can be complicated if your Objects have children or are otherwise contain other objects.
  5. You pass the array of copies into the method that does the stuff.
呢古 2024-10-14 23:51:17

如果您希望原始对象保持不变,则需要对原始对象的副本进行更改。这意味着数组的“深层复制”,您不仅创建引用相同对象的第二个数组,而且还复制数组中的每个对象。

根据谁应该拥有更改的对象(如果有的话),您可以在调用者中创建此副本并将其传入,或者传入原始数组并让该方法制作一组副本。第一个似乎更合乎逻辑,但在不了解更多关于您的设计的情况下很难说。

If you want the original objects to remain unchanged, you need to make the changes to copies of the original objects. This implies a "deep copy" of the array, where you're not just creating a second array with references to the same objects, but making copies of each object in the array as well.

Depending on who should own the changed objects (if anyone), you can create this copy in the caller and pass it in, or pass in the original array and have the method make a set of copies. The first one seems more logical, but without knowing more about your design it's hard to say.

她比我温柔 2024-10-14 23:51:17

您可以使用 Collections.copy(dest, src) 它将制作数组的深层副本。
但是,由于方法中存在错误,您应该采取一些技巧:

//Assuming, that src is an array

List<Integer> src2 = Arrays.asList(src)

List<Integer> dest = new ArrayList<Integer>(src2);

Collections.copy(dest, src2);

实际上,您不必传递源数组来构造目标数组。您必须传递一个与源数组大小相同的数组。

You may use Collections.copy(dest, src) it will make a deep copy of your array.
But, becase of a bug in the method, you should do some trick:

//Assuming, that src is an array

List<Integer> src2 = Arrays.asList(src)

List<Integer> dest = new ArrayList<Integer>(src2);

Collections.copy(dest, src2);

Actually, you dont have to pass the source array to construct the destiantion array. You have to pass an array with the same size as the source array.

走过海棠暮 2024-10-14 23:51:17

有多种方法可以阻止方法对作为参数传递的对象进行更改:

  • 制作对象的副本,然后传递该副本。复制对象的方法有很多种。复制构造函数和静态工厂方法可能比实现 clone() 更好。
  • 为对象的类创建一个只读包装类,实例化它并传递它。只读包装器要么不提供更新方法,要么实现它们不执行任何操作,或者引发异常。
  • 重构对象的 API,使其具有只读接口和读写接口,并将其作为前者的实例传递。 (当然,调用者可以尝试将其强制转换为后者...)
  • 使用 Java 安全性来防止不需要的更新。 (这是昂贵且复杂的......)

当然,如果您试图“保护”的对象具有您想要防止更改的深层结构,那么您需要确保处理这个问题;例如,通过深度复制、通过返回组件对象的副本或返回它们的包装等等。

上述大多数方法都会增加复杂性和运行时成本,因此我的建议是避免您需要执行此类操作的设计。如果可行,只需记录该对象应被视为只读,并将其留给编写该方法的人员的常识来执行正确的操作。

例如,众所周知的事实是,您不应更改用作 Map 中的键的对象的状态,以免其值发生变化。然而,通常不采取任何措施来尝试“强制”这一点;即使用可变对象作为键......并且只是不更改它们。

There are a number of approaches to stopping a method making changes to an object passed as a parameter:

  • Make a copy of the object, and pass the copy. There are a variety of ways of copying objects. Copy constructors and static factory methods are probably better than implementing clone().
  • Create a read-only wrapper class for the object's class, instantiate it and pass it. The read-only wrapper either doesn't provide the update methods, or implements them to do nothing, or throw exceptions.
  • Refactor the object's API so that it has an read-only interface and a read-write interface, and pass it as an instance of the former. (Of course, the caller can try to cast it to the latter ...)
  • Use Java security to prevent unwanted updates. (This is expensive and complicated ...)

Of course, if the object that you are trying to "protect" has deep structure that you want to prevent changes to, then you need to make sure that you deal with this; e.g. by deep copying, by returning copies of component objects or returning them wrapped, and so on.

Most of the approaches above add complexity and runtime cost, so my advice is to avoid designs where you need to do this kind of thing. If feasible, simply document that the object should be treated as read-only, and leave it to the common sense of the person who codes the method to do the right thing.

For example, it is a well known fact that you should not change the state of an object used as a key in a Map in such a way that its value changes. However, it is common to do nothing to try to "enforce" this; i.e. to use mutable objects as keys ... and just not change them.

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