这是重复局部变量问题的优雅解决方案吗?
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
之外,我还需要计算 e1
和 e2
。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
至少在两个方面,这是不优雅的代码:
newExchange
变量可以被声明为更深层次的嵌套。因此,在不改变任何行为的情况下,这是一个更好的版本:
接下来我们遇到的问题是,您没有告诉我们任何代码的用途,也没有告诉我们“重复的局部变量问题”的含义。哦,正如评论中指出的,你的循环永远不会终止。
This is inelegant code in at least two ways:
newExchange
variable could be declared more deeply nested.So without changing any behaviour, here's a nicer version:
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.
在不讨论您的代码的情况下,当您出现重复变量错误时,您始终可以使用 {}。
这不会编译
:
Without discussing your code, when you have duplicate variable errors, you can always use {}.
This does not compile
this does:
@Jon 的简化是最安全的,但我怀疑它可以进一步简化。
@Jon's simplification is the safest, however I suspect it can be simplified further.