aspose file tools*
The moose likes Beginning Java and the fly likes Copying objects from one Arraylist to another Arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copying objects from one Arraylist to another Arraylist" Watch "Copying objects from one Arraylist to another Arraylist" New topic
Author

Copying objects from one Arraylist to another Arraylist

Rudy Rodriguez
Greenhorn

Joined: May 01, 2009
Posts: 29
Hi

I created a arraylist of elements that contain variables and 2 fixed arrays, which I will call element arraylist.
I created a second arraylist which I will call location arraylist, the location arraylist will have information regarding a location, and also another arraylist of elements.

I want to copy objects from the element arraylist and place them in the second element arraylist in the location arraylist.

How do I copy objects of the element arraylist and place them into the element arraylist in the location arraylist ???

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Rudy,

if I understand you correctly you want to copy the entire source ArrayList?!?

You can call the method addAll() on your destination list which takes a collection and adds every element from it to the ArrrayList.

Marco
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Risky to copy like that because you are losing type safety. You have arrays and individual objects in the List; you can have problems with ClassCastExceptions when you retrieve things from the List.
Rudy Rodriguez
Greenhorn

Joined: May 01, 2009
Posts: 29
Hi Marco

I do not want to copy the entire arraylist to the location arraylist.
I want to copy from the element arraylist and place, the water/fuel tank objects into their particular locations in the location arraylist.
the location arraylist will be based on locations, such as Michigan, Ohio, Indiana, etc.
The second element arraylist will consists of water or fuel tanks, and the water and fuel tank info.

Michigan - location arraylist index 0
-- Michigan Water tank 1 -- element arraylist index 1
-- Michigan Water tank 2 -- element arraylist index 3
-- Michigan Fuel tank 1 -- element arraylist index 4

Ohio - location arraylist index 1
-- Ohio Water tank 1 -- element arraylist index 5
-- Ohio Water tank 2 -- element arraylist index 2
-- Ohio Fuel tank 1 -- element arraylist index 6

etc..
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Rudy, in the destination arraylist do you want a duplicate copy of the object, or just a reference to the same object? If the object in one arraylist were to change, would you want the copied object in the second arraylist to change also? This is a fundamental aspect of objects which must be understood.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Use the contains and add methods and you can copy things from one List to another. If you have locations and Lists, maybe a Map<Location, List><Tank>> would work better. How much do you know about the Collections framework?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Copying objects from one Arraylist to another Arraylist