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

时间:2022-08-22 22:34:22

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 x and 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中是否有一种方法可以在不同的实例中重用相同的名称?

ADDED I need to clarify some issues. I did not show all the code. I have the break command in the loop (when a new value cannot be generate, I exit the loop). x and value have Integer[] type.

我还需要澄清一些问题。我没有显示所有的代码。我在循环中有break命令(当一个新值不能生成时,我退出循环)。x和值具有整数[]类型。

ADDED 2 Since 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:

添加了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 values list I had identical elements (and I know that in the loop on every cycle x had a new value).

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

ADDED 3

增加了3

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

为什么我列表中的所有元素看起来都一样?

1 个解决方案

#1


2  

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 add method).

它打印从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 x everytime, 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,因为你不会在循环中使用它。

#1


2  

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 add method).

它打印从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 x everytime, 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,因为你不会在循环中使用它。