为什么我得到一个“重复局部变量”错误? [英] Why do I get a "duplicate local variable" error?
问题描述
x = getValue()
values.add(x)
while(true){
x = getValue();
values.add(x)
}
我发现这种方法因为我将相同的实例添加到列表中,所以不起作用。更详细的说,在循环的每一个循环中,我重新分配一个新的值到 x
,这样做我改变了已经添加到列表中的所有元素的值所以最终得到一个相同元素的列表。)
为了解决这个问题,我做了以下工作:
x = getValue();
整数[] valueToAdd = new Integer [n]; (int i = 0; i< n; i ++)
{
valueToAdd [i] = x [i];
}
while(true){
x = getValue();
y = new Integer [n]; (int i = 0; i< n; i ++)
{
valueToAdd [i] = x [i];
}
values.add(valueToAdd)
}
这样我想创建一个新的实例,每次想要添加一个值到列表。但是它没有工作,因为我得到一个重复的局部变量错误。
我也很奇怪,如果我多次声明相同的变量,我没有这个错误在循环。只有在我首先在循环之外声明一个新的变量,然后在循环中声明这个问题,才会出现这个问题。
Java中是否有一种方法来重用同一个名字不同的实例?
ADDED
我需要澄清一些问题。我没有显示所有的代码。我在循环中有 break
命令(当一个新值不能生成时,我退出循环)。 x
和值
有整数[]
类型。 p>
ADDED 2
由于提到问题可能在 getValue()
我需要在这里更多的细节。实际上我的代码中没有 getValue()
(我使用 getValue()
来缩小我的例子) 。在我的代码中,我有:
整数[] x = new x [n];
while(true){
for(int i = 0; i< n; i ++){
x [i] = y [i]
}
values.add(x)
}
它没有工作,因为在我的值
列表中,我有相同的元素(我知道在每个循环的循环中 x
有一个新值)
添加3
你的问题不是你的想法。例如看一下这个简单的程序:
String x = null;
列表< String> l = new ArrayList< String>(); (int i = 0; i< 10; i ++){
x = String.valueOf(i);
l.add(x);
}
System.out.println(l);
它打印从0到9的数字。这是因为java是pass-by-value(< a href =http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html =nofollow>查看这里)。你没有传递x的引用,你传递x的值(在 add
方法中)。
所以问题在于$ code> getValue()方法,返回相同的对象。
更新:现在这个问题更有意义了。您每次都使用相同的对象 x
,只是更改其状态。为了使不同的值只是在循环中移动声明:
while(true){
整数[] x = new x [n];
...
}
如果在循环之外需要它, ,只需在其中使用另一个变量。它不必被命名为 x
。因为你不会在循环内使用它。
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).
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?
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.
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:
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).
ADDED 3
Why all elements of my list seems to be the same?
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).
So the problem lies in the getValue()
method, which returns the same object.
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:
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.
这篇关于为什么我得到一个“重复局部变量”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!