java - Different ways to copy an ArrayList -


is there difference in between these three, assuming a has type arraylist<t>? (aside fact compiler complains unchecked operations in #3.)

1.

arraylist<t> a1 = new arraylist<t> (a); 

2.

arraylist<t> a2 = new arraylist<t> (); a2.addall (a); 

3.

arraylist<t> a3 = (arraylist<t>) (a.clone()); 

cloning creates new instance, holding same elements. clone works fine collections. better not use them.

arraylist<t> a1 = new arraylist<t> (a); 

is shallow copy , comparatively faster.

this thread may further


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -