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.
}
我需要计算e1
,e2
另外还要计算e
.