• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Copying objects from one Arraylist to another Arraylist

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???

 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic