Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Copying ArrayList

Reply
Thread Tools

Copying ArrayList

 
 
ruds
Guest
Posts: n/a
 
      06-06-2007
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?

 
Reply With Quote
 
 
 
 
Abhishek Srivastava
Guest
Posts: n/a
 
      06-06-2007
On Jun 6, 2:06 pm, ruds <(E-Mail Removed)> wrote:
> 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?


http://java.sun.com/developer/JDCTec...01/tt0410.html

- Abhi

 
Reply With Quote
 
 
 
 
ruds
Guest
Posts: n/a
 
      06-06-2007
Thanks a lot..

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-15-2007
On Tue, 05 Jun 2007 22:06:34 -0700, ruds <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>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



--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clear Arraylist vs new ArrayList Philipp Java 6 05-28-2008 09:07 PM
Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList? xz Java 16 08-04-2007 10:33 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments