Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copying objects from one Arraylist to another Arraylist

 
Rudy Rodriguez
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50270
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50270
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic