Copying ArrayList

R

ruds

hi,
I have 2 arraylists al1 and al2. I'm copying al1 to al2 i.e.
al2=al1;
if i'm removing some elements from al2, the same elements from al1 too
are removed.
why so?
How do i prevent it?
How to copy array list can i clone it?
 
R

Roedy Green

hi,
I have 2 arraylists al1 and al2. I'm copying al1 to al2 i.e.
al2=al1;
if i'm removing some elements from al2, the same elements from al1 too
are removed.
why so?
How do i prevent it?
How to copy array list can i clone it?

all copying with

arraylist2 = arraylist1

does in give you two pointers to the same ArrayList.

You need to clone the ArrayList.

See if clone is defined.

See if there is a constructor that takes another ArrayList or List.

In a pinch, export an array and use that to create a new ArrayList.

see http://mindprod.com/jgloss/arraylist.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top