java中重用变量的问题

发布于 2024-11-24 10:17:06 字数 1090 浏览 1 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(3

起风了 2024-12-01 10:17:06

这个问题与无关变量有关。它是关于对象和它们的变异

因此,根据需要创建一个新对象。 “问题”是存储在 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.

浅浅淡淡 2024-12-01 10:17:06

之后,我必须重用临时变量,所以我需要
重新初始化它。我按照以下说明进行操作:
for(BooleanWrap bool: temp)
{ bool.set(false); }

不会重新初始化临时变量。这会重新初始化临时变量的内容。要重新初始化临时变量,您的代码将如下所示

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:

temp = new ArrayList <BooleanWrap>(48); 
//cycle: operations to fill the ArrayList

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); }

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

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:

temp = new ArrayList <BooleanWrap>(48); 
//cycle: operations to fill the ArrayList
南冥有猫 2024-12-01 10:17:06

使用 BooleanWRap 效率非常低,即使是 Boolean 也效率低下。

相反,我建议您使用 BitSet。

BitSet temp = new BitSet(48);

//cycle: operations to fill the ArrayList, then a condition is satisfied

hashMap.put(index, temp);

// to create a new BitSet...
temp = new BitSet(48);

Using a BooleanWRap is very inefficient, even a Boolean would be inefficient.

Instead I suggest you use a BitSet.

BitSet temp = new BitSet(48);

//cycle: operations to fill the ArrayList, then a condition is satisfied

hashMap.put(index, temp);

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