This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 ???
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.
Joined: May 01, 2009
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
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.
Joined: Oct 13, 2005
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?