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

时间:2021-11-24 16:48:49
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.

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

ADDED 2

添加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.

我还需要计算e1和e2来计算e。

3 个解决方案

#1


4  

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.
  • 您的newExchange变量可以声明为更深入的嵌套。

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.

接下来的问题是,您没有告诉我们这些代码应该做什么,也没有告诉我们“重复的局部变量问题”是什么意思。正如在评论中指出的那样,你的循环永远不会终止。

#2


3  

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

#3


2  

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

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

#1


4  

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.
  • 您的newExchange变量可以声明为更深入的嵌套。

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.

接下来的问题是,您没有告诉我们这些代码应该做什么,也没有告诉我们“重复的局部变量问题”是什么意思。正如在评论中指出的那样,你的循环永远不会终止。

#2


3  

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

#3


2  

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

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