Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top