java中重用变量的问题
我在 java 中的引用分配时遇到了一些问题。
让我解释一下:在我的代码中,我使用 BooleanWrap (一个自制的布尔类)的 ArrayList,稍后将其指定为 HashMap 中的值(有一个 for 循环,它更新此 ArrayList 的值,直到条件已验证)。
像这样的事情:
ArrayList<BooleanWrap> temp=new ArrayList <BooleanWrap>(48);
//cycle: operations to fill the ArrayList, then a condition is satisfied
hashMap.put(index, temp);
之后,我必须重用临时变量,所以我需要重新初始化它。我按照以下说明进行操作:
for(BooleanWrap bool: temp){
bool.set(false);
}
我的问题来了:将 temp 指定为 hashMap 的值将仅保存变量的引用,而不是实际值。
因此,重新初始化 temp 也会导致哈希映射内部的更新(在这种情况下,将所有内容设置为 false)。
现在,我认为即使使用 clone() 方法,我也应该得到相同的结果(因为它生成 ArrayList 的浅表副本)。
有没有办法在我的循环中重用相同的变量 temp,而无需稍后将其引用分配给哈希图?
实际上,我可以使用创建数组列表的深层副本的方法来完成此操作:
public static ArrayList<BooleanWrap> deepcopy(ArrayList<BooleanWrap> original){
ArrayList<BooleanWrap> copy=new ArrayList<BooleanWrap>(48);
for(BooleanWrap bool: original)
copy.add(new BooleanWrap (bool.get()));
return copy;
}
但我需要内联完成一些操作,无需任何其他方法,并且尽可能短。
有什么建议/建议/侮辱吗?
I got some problem with reference assignment in java.
Let me explain: in my code, i'm using an ArrayList of BooleanWrap (a home made class of boolean) which will be later assigned as a value in a HashMap (there is a for cycle which update the values of this ArrayList until a condition is verified).
Something like this:
ArrayList<BooleanWrap> temp=new ArrayList <BooleanWrap>(48);
//cycle: operations to fill the ArrayList, then a condition is satisfied
hashMap.put(index, temp);
After that, I have to reuse the temp variable, so I need to reinitialize it. I do it with the following instructions:
for(BooleanWrap bool: temp){
bool.set(false);
}
There comes my problem: assigning temp as a value of the hashMap will save only the reference of the variable, not the actual value.
So, reinitializing temp cause also the updating inside the hashmap (in this case, setting everything false).
Now, I think that even with clone() method I should get the same result (cause it produces a shallow copy of the ArrayList).
Is there a way to reuse the same variable temp in my cycle without assign later the reference of it to the hashmap?
Actually I can do it with a method which creates a deep copy of the arraylist:
public static ArrayList<BooleanWrap> deepcopy(ArrayList<BooleanWrap> original){
ArrayList<BooleanWrap> copy=new ArrayList<BooleanWrap>(48);
for(BooleanWrap bool: original)
copy.add(new BooleanWrap (bool.get()));
return copy;
}
but I need something done inline, without any other methods and as shorter as it can be.
Any advice/suggestion/insult?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题与无关与变量有关。它是关于对象和它们的变异。
因此,根据需要创建一个新对象。 “问题”是存储在 HashMap 中的原始对象正在发生变化。
然而,避免重复使用变量可以(通常吗?)使代码更易于维护和理解。
快乐编码。
The issue is not about variables. It is about objects and mutations upon them.
So create a new object as needed. The "issue" is that the original object stored in the HashMap is being mutated.
Avoiding re-use of variables, however, can (often does?) make code much more maintainable and understandable.
Happy coding.
不会重新初始化临时变量。这会重新初始化临时变量的内容。要重新初始化临时变量,您的代码将如下所示
That does not re-initialize the temp variable. That re-initializes the contents of your temp variable. to re-initialize the temp variable your code would look like
使用 BooleanWRap 效率非常低,即使是 Boolean 也效率低下。
相反,我建议您使用 BitSet。
Using a BooleanWRap is very inefficient, even a Boolean would be inefficient.
Instead I suggest you use a BitSet.