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 120513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:49:24+00:00 2026-05-11T03:49:24+00:00

Ok, here is the code and then the discussion follows: public class FlatArrayList {

  • 0

Ok, here is the code and then the discussion follows:

public class FlatArrayList {      private static ArrayList<TestWrapperObject> probModel = new ArrayList<TestWrapperObject>();      /**      * @param args      */     public static void main(String[] args) {         // TODO Auto-generated method stub          int [] currentRow = new int[10];          int counter = 0;          while (true) {            for (int i = 0; i < 10; i++) {             currentRow[i] = probModel.size();           }            TestWrapperObject currentWO = new TestWrapperObject(currentRow);            probModel.add(counter, currentWO);            TestWrapperObject testWO = probModel.get(counter);           // System.out.println(testWO);            counter++;            if (probModel.size() == 10) break;         }         // Output the whole ArrayList            for (TestWrapperObject wo:probModel) {          int [] currentTestRow = wo.getCurrentRow();        }      } }  public class TestWrapperObject {      private int [] currentRow;      public void setCurrentRow(int [] currentRow) {         this.currentRow = currentRow;     }      public int [] getCurrentRow() {         return this.currentRow;     }      public TestWrapperObject(int [] currentRow) {         this.currentRow = currentRow;     }  } 

What is the above code supposed to do? What I am trying to do is load an array as a member of some wrapper object (TestWrapperObject in our case). When I get out of the loop, the probModel ArrayList has the number of elements it is supposed to have but all have the same value of the last element (an array of size 10 with each item equal to 9). This is not the case inside the loop. If you perform the same ‘experiment’ with a primitive int value everything works fine. Am I missing something myself regarding arrays as object members? Or did I just encounter a Java bug? I am using Java 6.

  • 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. 2026-05-11T03:49:24+00:00Added an answer on May 11, 2026 at 3:49 am

    You are only creating one instance of the currentRow array. Move that inside the row loop and it should behave more like you expect.

    Specifically, the assignment in setCurrentRow does not create a copy of the object, but only assigns the reference. So each copy of your wrapper object will hold a reference to the same int[] array. Changing the values in that array will make the values appear to change for all other wrapper objects that hold a reference to the same instance of the array.

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

Sidebar

Related Questions

I'm trying to make things simpler. Here is my code: If Threading.Monitor.TryEnter(syncRoot) Then Try
I've got this code here: SqlCommand CodeStatus = new SqlCommand(SQL, DB); DB.Open(); Reader =
I'm wondering the best form for my constructors. Here is some sample code: class
Edit: The code here still has some bugs in it, and it could do
See here: http://code.google.com/p/ie7-js/ Does anyone have any experience or remarks about this javascript? Is
I have this code here: var infiltrationResult; while(thisOption) { var trNode = document.createElement('tr'); var
We have some old C code here that's built with nmake. Is there an
I am using pseudo-code here, but this is in JavaScript. With the most efficient
I have a piece of code here that i really could use some help
I am compiling a legacy C code here and there is a lot of

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.