这是重复局部变量问题的优雅解决方案吗?

发布于 2024-10-05 10:19:49 字数 1540 浏览 4 评论 0原文

Integer[] lastExchange = new Integer[nColors];
Integer[] exchangeToAdd = new Integer[nColors];
lastExchange = getValue();
exchangeToAdd = getValue(); 
exchanges.add(exchangeToAdd);

Integer[] newExchange = new Integer[nColors];
while (true) {
   newExchange = getValue(lastExchange);
   Integer[] exchangeToAddForLoop = new Integer[nColors];
   for (int i=0; i<nColors; i++) {
      lastExchange[i] = newExchange[i];
      exchangeToAddForLoop[i] = newExchange[i];
  }
  exchanges.add(exchangeToAddForLoop);
}

添加

我想用这段代码做什么?我需要填充(填写)名为 exchanges 的列表。列表的第一个元素是lastExchange。我的代码问题是我总是需要创建一个变量的两个副本(这就是为什么我认为代码不优雅但我找不到更好的解决方案)。例如,一开始我创建 lastExchange,然后创建 exchangeToAdd(与 lastExchange 具有相同的值)。循环中也会发生同样的情况。我创建 lastExchange,然后创建 exchangeToAddForLoop。我这样做是因为我无法将 lastExchange 添加到列表中,因为它稍后会被修改。

添加2

这是我的问题。我有这样的代码:

Integer[] e  = getValue();
Integer[] e1 = getValue();  // <-- I do not like that.
exchanges.add(e1);          // <-- I do not like that.
while (true) {
   Integer[] e_new = getValue(e);
   Integer[] e2 = new Integer[nColors]; // <-- I do not like that.
   for (int i=0; i<nColors; i++) {
      e[i] = e_new[i];
      e2[i] = e_new[i]; // <-- I do not like that.
  }
  exchanges.add(e2); // <-- I do not like that.
}

除了计算 e 之外,我还需要计算 e1e2

Integer[] lastExchange = new Integer[nColors];
Integer[] exchangeToAdd = new Integer[nColors];
lastExchange = getValue();
exchangeToAdd = getValue(); 
exchanges.add(exchangeToAdd);

Integer[] newExchange = new Integer[nColors];
while (true) {
   newExchange = getValue(lastExchange);
   Integer[] exchangeToAddForLoop = new Integer[nColors];
   for (int i=0; i<nColors; i++) {
      lastExchange[i] = newExchange[i];
      exchangeToAddForLoop[i] = newExchange[i];
  }
  exchanges.add(exchangeToAddForLoop);
}

ADDED

What I am trying to do with this code? I need to populate (fill in) the list called exchanges. The first element of the list is lastExchange. My problem with the code is that I always need to create two duplicates of an variable (it is why I think that the code is not elegant but I cannot find a better solution). For example, in the very beginning I create lastExchange and then I create exchangeToAdd (that has the same value as lastExchange). The same happens in the loop. I create lastExchange and then I create exchangeToAddForLoop. I do so because I cannot add lastExchange to the list because it will be modified latter.

ADDED 2

Here is my problem. I have the code like that:

Integer[] e  = getValue();
Integer[] e1 = getValue();  // <-- I do not like that.
exchanges.add(e1);          // <-- I do not like that.
while (true) {
   Integer[] e_new = getValue(e);
   Integer[] e2 = new Integer[nColors]; // <-- I do not like that.
   for (int i=0; i<nColors; i++) {
      e[i] = e_new[i];
      e2[i] = e_new[i]; // <-- I do not like that.
  }
  exchanges.add(e2); // <-- I do not like that.
}

and I need to calculate e1 and e2 additionally to the calculation of e.

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

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

发布评论

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

评论(3

花心好男孩 2024-10-12 10:19:49

至少在两个方面,这是不优雅的代码:

  • 大多数局部变量都被分配了值,然后这些值立即被覆盖
  • 您的 newExchange 变量可以被声明为更深层次的嵌套。

因此,在不改变任何行为的情况下,这是一个更好的版本:

Integer[] lastExchange = getValue();
Integer[] exchangeToAdd = getValue();
exchanges.add(exchangeToAdd);

while (true) {
   Integer[] newExchange = getValue(lastExchange);
   Integer[] exchangeToAddForLoop = new Integer[nColors];
   for (int i=0; i<nColors; i++) {
      lastExchange[i] = newExchange[i];
      exchangeToAddForLoop[i] = newExchange[i];
   }
   exchanges.add(exchangeToAddForLoop);
}

接下来我们遇到的问题是,您没有告诉我们任何代码的用途,也没有告诉我们“重复的局部变量问题”的含义。哦,正如评论中指出的,你的循环永远不会终止。

This is inelegant code in at least two ways:

  • Most of your local variables are being assigned values which are then immediately overwritten
  • Your newExchange variable could be declared more deeply nested.

So without changing any behaviour, here's a nicer version:

Integer[] lastExchange = getValue();
Integer[] exchangeToAdd = getValue();
exchanges.add(exchangeToAdd);

while (true) {
   Integer[] newExchange = getValue(lastExchange);
   Integer[] exchangeToAddForLoop = new Integer[nColors];
   for (int i=0; i<nColors; i++) {
      lastExchange[i] = newExchange[i];
      exchangeToAddForLoop[i] = newExchange[i];
   }
   exchanges.add(exchangeToAddForLoop);
}

Next we come to the problem that you haven't told us what any of this code is meant to be doing, nor what you mean by "the duplicate local variable problem". Oh, and as pointed out in the comments, your loop never terminates.

天涯沦落人 2024-10-12 10:19:49

在不讨论您的代码的情况下,当您出现重复变量错误时,您始终可以使用 {}。

这不会编译

            int a=0;
            a++;


            int a=0;
            a++;

        {
            int a=0;
            a++;
        }
        {
            int a=0;
            a++;
        }

Without discussing your code, when you have duplicate variable errors, you can always use {}.

This does not compile

            int a=0;
            a++;


            int a=0;
            a++;

this does:

        {
            int a=0;
            a++;
        }
        {
            int a=0;
            a++;
        }
执手闯天涯 2024-10-12 10:19:49

@Jon 的简化是最安全的,但我怀疑它可以进一步简化。

exchanges.add(getValue());

while (true) { // forever??
   // do you need null values or can you use int[]
   int[] newExchange = getValue(exchanges.get(exchanges.size()-1);
   // do you need to add a copy, if not then clone() can be dropped.
   exchanges.add(newExchange.clone());
}

@Jon's simplification is the safest, however I suspect it can be simplified further.

exchanges.add(getValue());

while (true) { // forever??
   // do you need null values or can you use int[]
   int[] newExchange = getValue(exchanges.get(exchanges.size()-1);
   // do you need to add a copy, if not then clone() can be dropped.
   exchanges.add(newExchange.clone());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文