java:传递引用数组?
我有对象集合数组,我将此引用传递给方法,该方法将执行一些业务逻辑并更新对象,因此它将反映在原始对象中,因为我将其作为引用传递。现在我的问题是如何避免原始对象的更新。
如果涉及克隆,请让我知道我需要为上述案例进行哪种克隆
,谢谢
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的情况有点棘手:
你有一个对象的引用数组。
Java 总是按值传递,因此当您将数组的引用传递到方法中时,对数组的引用是按值传递的。
因此该方法有一个对与调用范围相同的数组的新引用。
您问题的答案是:如果您不希望更改反映在对象中,则需要复制对象。所以,要经历它。
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.
如果您希望原始对象保持不变,则需要对原始对象的副本进行更改。这意味着数组的“深层复制”,您不仅创建引用相同对象的第二个数组,而且还复制数组中的每个对象。
根据谁应该拥有更改的对象(如果有的话),您可以在调用者中创建此副本并将其传入,或者传入原始数组并让该方法制作一组副本。第一个似乎更合乎逻辑,但在不了解更多关于您的设计的情况下很难说。
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.
您可以使用 Collections.copy(dest, src) 它将制作数组的深层副本。
但是,由于方法中存在错误,您应该采取一些技巧:
实际上,您不必传递源数组来构造目标数组。您必须传递一个与源数组大小相同的数组。
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:
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.
有多种方法可以阻止方法对作为参数传递的对象进行更改:
clone()
更好。当然,如果您试图“保护”的对象具有您想要防止更改的深层结构,那么您需要确保处理这个问题;例如,通过深度复制、通过返回组件对象的副本或返回它们的包装等等。
上述大多数方法都会增加复杂性和运行时成本,因此我的建议是避免您需要执行此类操作的设计。如果可行,只需记录该对象应被视为只读,并将其留给编写该方法的人员的常识来执行正确的操作。
例如,众所周知的事实是,您不应更改用作
Map
中的键的对象的状态,以免其值发生变化。然而,通常不采取任何措施来尝试“强制”这一点;即使用可变对象作为键......并且只是不更改它们。There are a number of approaches to stopping a method making changes to an object passed as a parameter:
clone()
.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.