Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3460250
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:12:24+00:00 2026-05-18T10:12:24+00:00

I have a loop in which I calculate a value and add it it

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:12:24+00:00Added an answer on May 18, 2026 at 10:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a for-loop which is setting the value of the progress bar on
I have a loop which basically calls this every few seconds (after the timeout):
I have a loop which needs to create an unspecified and indefinite amount of
I have a loop MC which will be duplicate to stage several times according
I have a for-loop which performs the following function: Take a M by 8
i have a php loop which displays only one record even if there is
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I have a loop in razor which generates a template a number of times.
I have a binary search loop which gets hit many times in the execution
I have a website and a script with a for loop which uses the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.