java 为什么会出现“重复的局部变量”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4344051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:58:41  来源:igfitidea点击:

Why do I get a "duplicate local variable" error?

javaduplicateslocal-variables

提问by Roman

I have a loop in which I calculate a value and add it it a list. So, I do something like that:

我有一个循环,在其中计算一个值并将其添加到列表中。所以,我做这样的事情:

x = getValue()
values.add(x)
while (true) {
   x = getValue();
   values.add(x)
}

I found out that this approach does not work since I add the same instance to the list. In more details, in every cycle of the loop I re-assign a new value to the xand doing so I change values of all elements that were already added to the list (so in the end I get a list of identical elements).

我发现这种方法不起作用,因为我将相同的实例添加到列表中。更详细地说,在循环的每个循环中,我都会为 重新分配一个新值x,这样做我会更改已添加到列表中的所有元素的值(因此最后我得到了相同元素的列表)。

To solve this problem I did the following:

为了解决这个问题,我做了以下事情:

x = getValue();
Integer[] valueToAdd = new Integer[n];
for (int i=0; i<n; i++) {
   valueToAdd[i] = x[i];
}
while (true) {
   x = getValue();
   y = new Integer[n];
   for (int i=0; i<n; i++) {
      valueToAdd[i] = x[i];
   }
   values.add(valueToAdd)
}

In this way I wanted to create a new instance every time want to add a value to the list. But it does not work since I get a duplicate local variable error.

通过这种方式,我想在每次想要向列表中添加一个值时创建一个新实例。但它不起作用,因为我收到重复的局部变量错误。

It is also strange to me that I do not have this error if I declare the same variable many times in the loop. The problem appears only if I first declare a new variable outside the loop and then also in the loop.

我也很奇怪,如果我在循环中多次声明相同的变量,我没有出现这个错误。仅当我首先在循环外声明一个新变量,然后也在循环中声明一个新变量时才会出现问题。

Is there a way in Java to re-use the same name for different instances?

Java 中有没有办法为不同的实例重用相同的名称?

ADDEDI need to clarify some issues. I did not show all the code. I have the breakcommand in the loop (when a new value cannot be generate, I exit the loop). xand valuehave Integer[]type.

添加我需要澄清一些问题。我没有显示所有代码。我break在循环中有命令(当无法生成新值时,我退出循环)。xvalueInteger[]类型。

ADDED 2Since it was mentioned that the problem can be in the getValue()I need to in more details here. Actually I do not have getValue()in my code (I used getValue()here to make my example shorter). In my code I had:

ADDED 2因为有人提到问题可能出在getValue()我需要在这里更详细的地方。实际上getValue()我的代码中没有(我getValue()在这里使用来使我的示例更短)。在我的代码中,我有:

   Integer[] x = new x[n];
    while (true) {
       for (int i=0; i<n; i++) {
          x[i] = y[i];
       }
       values.add(x)
    }

And it did not work since in my valueslist I had identical elements (and I know that in the loop on every cycle xhad a new value).

它不起作用,因为在我的values列表中我有相同的元素(我知道在每个循环的循环中x都有一个新值)。

ADDED 3

添加 3

Why all elements of my list seems to be the same?

为什么我的列表中的所有元素似乎都相同?

回答by Bozho

Your problem is not what you think it is. For example take a look at this simple program:

你的问题不是你认为的那样。例如看看这个简单的程序:

String x = null;
List<String> l = new ArrayList<String>();
for (int i = 0; i < 10; i ++) {
    x = String.valueOf(i);
    l.add(x);
}

System.out.println(l);

It prints the numbers from 0 to 9. This is because java is pass-by-value (check here). You are not passing the reference to x, you are passing the value of x (in the addmethod).

它打印从 0 到 9 的数字。这是因为 java 是按值传递的(检查这里)。您没有传递对 x 的引用,而是传递 x 的值(在add方法中)。

So the problem lies in the getValue()method, which returns the same object.

所以问题在于getValue()方法,它返回相同的对象。

Update:Now the question makes more sense. You are working with the same object xeverytime, and just changing its state. In order to put different values just move the declaration inside the loop:

更新:现在这个问题更有意义。您x每次都在使用同一个对象,只是改变了它的状态。为了放置不同的值,只需在循环内移动声明:

while (true) {
   Integer[] x = new x[n];
   ...
}

If you need it outside the loop, well, simply use another variable there. It does not have to be named x. Since you won't be using it inside the loop anyway.

如果您在循环之外需要它,那么,只需在那里使用另一个变量即可。它不必被命名x。因为无论如何你都不会在循环内使用它。