File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to add a an object to a arraylist without overwriting other objects in the arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to add a an object to a arraylist without overwriting other objects in the arraylist" Watch "How to add a an object to a arraylist without overwriting other objects in the arraylist" New topic
Author

How to add a an object to a arraylist without overwriting other objects in the arraylist

Rudy Rodriguez
Greenhorn

Joined: May 01, 2009
Posts: 29
Hi
I am working on a program that will read input from a Excel file
The input will be placed into a object that I created called Entity.
The object will then be placed into a a arraylist;

I Run the program and it creates the correct number of Entity objects in the arraylist.
The objects are created and the correct information is being placed inside them.
However the objects in the arraylist are being overwritten by the last object added.

How do I stop from overwriting all the objects in the list ??


I am attaching the code below...

ArrayList<Entity> elist = new ArrayList<Entity>();
Entity s = new Entity();


while(!temp.isEmpty())
{
d0 = sheet1.getCell(3,cell_index);
temp2 = d0.getContents();

if(!temp2.isEmpty())
{
//System.out.println("The unit has logic : " + temp2);
d0 = sheet1.getCell(3,cell_index);
temp2 = d0.getContents();

b0 = sheet1.getCell(1, cell_index);
con_type = b0.getContents();

c0 = sheet1.getCell(2, cell_index);
Iquant = c0.getContents();

s = new Entity();
s.SetName(temp);
s.SetType(con_type);
s.SetInitWater(Integer.parseInt(Iquant));

System.out.println("Name : " + s.GetName());

elist.add(s);

}

a0 = sheet1.getCell(0,cell_index++);
temp = a0.getContents();

}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Welcome to JavaRanch and please use the code button because it makes your code much easier to read.

If you are adding to a List, you need to read what its add() method does, and how ArrayList (which is of course a kind of List, implements the add() method.

And where are you declaring the eList reference? Is it a field or local variable? That might be the real problem.
Rudy Rodriguez
Greenhorn

Joined: May 01, 2009
Posts: 29
I declared the elist variable as a global variable.
The Entity object is declared in the method.


Kim Gaskins
Greenhorn

Joined: Apr 30, 2009
Posts: 5
I'm new at this but I wonder if display tags would be useful for you. In our shop, when we're dealing with displaying arrays of information, we use display tags which handling paging, sorting, links and a lot of other stuff I've yet to learn. Just go to http://displaytag.sourceforge.net/11/index.html and look at some examples.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

Rudy Rodriguez wrote:


This is different than the code you posted in your first post, and the difference is your problem.

You make on instance of Entity named s. You fill it, you put it in the ArrayList. You keep the same instance of Entity, you change its values, and you put it into the ArrayList again. You then repeat...

So what have at the end is 1 Entity, placed into ArrayList several times. Since there is only one Entity created, then all the entries in the ArrayList have the same values (they are, after all, the same Object).

Why did you comment out the s = new Entry(); line? It wasn't commented in the original post. Uncomment that code and your problem will be solved. In this scenario, s is assigned a New Entity instance each round through the loop. This new instance is filled, and added to the ArrayList. Which means each entry in the ArrayList is a different Object, and so can have different values.


Steve
Rudy Rodriguez
Greenhorn

Joined: May 01, 2009
Posts: 29
Hi Steve
Thanks for the input.
I have been trying different variations of code to try to fix my problem.
I Commented out the line due to it had no affect on my output.
Meaning that the Entity object overwrote the other objects in the arraylist even with the s = new Entity(); commented or uncommented out.
I appreciate any helpful advice.
Thanks..

I am copying and pasting my output with the s = new Entity(); line uncommented.

  • init:
    deps-jar:
    compile:
    run:
    ScenarioData
    Number of Sheets in Workbook : 32
    Scenario time is : 24
    This sheet is SimpleProviderConsumables
    The number of rows in this sheet are : 47
    The number of Entities in this scenario is : 12

    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    ArrayList : PB_Sniper_FUELTANK
    BUILD SUCCESSFUL (total time: 6 seconds)




  • John de Michele
    Rancher

    Joined: Mar 09, 2009
    Posts: 600
    Rudy:

    Since you're generating a lot of Entity objects, you might want to add a factory method to Entity:


    Then you can change your read code to this:


    John.
    Steve Luke
    Bartender

    Joined: Jan 28, 2003
    Posts: 3947
        
      17

    In that case:

    1) Are you using any static variables in the Entity?
    2) Are you sure you are getting different values in from the source? Print out the values your are storing inside the Loop to make sure they change.
    Rudy Rodriguez
    Greenhorn

    Joined: May 01, 2009
    Posts: 29
    Thanks.
    I implemented the entity generator method and my program works..

    Thanks for your help...

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to add a an object to a arraylist without overwriting other objects in the arraylist
     
    Similar Threads
    How to delete from the middle of the file using object streams
    Hash tables
    inner class?? (Topic deleted by mistake)
    Using Comparable, Generics, and recursion to sort an array alphabetically
    Java - Works, but doesnt work...