This discussion is archived
4 Replies Latest reply: Jan 25, 2007 5:26 PM by 807607 RSS

ArrayList Problem

807607 Newbie
Currently Being Moderated
Hi all,

I have an ArrayList that holds calculations of the real data, and I am trying to retrieve them according to corresponding year. For example, I have selected 3 years such as 2000, 2001, 2003. I want to display each year's data in individual column. Instead I am getting the data of the 2003 and copied over to the rest of the columns.

Here is my code
ArrayList list = new ArrayList()
           for(int i=0; i<years.length; i++){
           list.add("RD1");
           list.add("RD2");
           list.add("RD3");
           if(list == null){
            display the proper error message;
           }else{
                do the calculations;
           }
           }
Thanks
  • 1. Re: ArrayList Problem
    800323 Newbie
    Currently Being Moderated
    Hi all,

    I have an ArrayList that holds calculations of the
    real data, and I am trying to retrieve them according
    to corresponding year. For example, I have selected 3
    years such as 2000, 2001, 2003. I want to display
    each year's data in individual column. Instead I am
    getting the data of the 2003 and copied over to the
    rest of the columns.

    Here is my code
    ArrayList list = new ArrayList()
    for(int i=0; i<years.length; i++){
    list.add("RD1");
    list.add("RD2");
    list.add("RD3");
    if(list == null){
    display the proper error message;
    }else{
    do the calculations;
    }
    }
    Your sample code does not explain the problem. Show a small version of your program that can run, show the output, and show what you expect.
  • 2. Re: ArrayList Problem
    807607 Newbie
    Currently Being Moderated
    I would guess you're running into a reference/value problem -- remember that when you add a member to the ArrayList, you're adding a reference. If you simply overwrite the value of a reference you've added, then add the reference to the list, you've just added another reference to the new value.
  • 3. Re: ArrayList Problem
    807607 Newbie
    Currently Being Moderated
    That's exactly my problem. Can you give me a programic example (code) to avoid such problem?

    Thanks,
  • 4. Re: ArrayList Problem
    807607 Newbie
    Currently Being Moderated
    Here is what I am getting now:
    Year1=2000 Year2=2001 Year3=2002     Year4=2004
    85.78     85.78     85.78     85.78
    45     45     45     45
    67     67     67     67
    120     120     120     120
    And Here's what I should get
    Year1=2000 Year2=2001 Year3=2002     Year4=2004
    65     35     98     89
    45     20     30     78
    32     160     87     100
    48     98     75     65