Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > shallow copying an ArrayList in Java 5

Reply
Thread Tools

shallow copying an ArrayList in Java 5

 
 
Gary Newell
Guest
Posts: n/a
 
      12-13-2004
I need some help with Java 5.

The code snippet below worked fine in Java 1.4.2. My code makes an array
list of some predefined strings. The strings do not change, but the strings
can belong (sometime concurrently) to various groups. In Java 1.4.2, I
created the new group by copying an existing group and then modifying the
group's contents.

In Java 5, how do I "shallow copy" an ArrayList?

code:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
import java.util.*;

public class cloneArrayList {

public static void main(String args[]) {

ArrayList<String> original = new ArrayList<String>();
ArrayList<String> shallowCopy;

// initialize the array list
original.add( "StrA" );
original.add( "StrB" );
original.add( "StrC" );

// shallow copy the array list
shallowCopy = (ArrayList) original.clone();

for( int j=0; j<original.size(); j++ ) {
System.out.println( "j=" + j + ", str=" + shallowCopy.get( j ) );
}
}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Output:
C:\temp>java cloneArrayList
j=0, str=StrA
j=1, str=StrB
j=2, str=StrC
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Compiler warning:
C:\temp>javac cloneArrayList.java
Note: cloneArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C:\temp>javac -Xlint:unchecked cloneArrayList.java
cloneArrayList.java:16: warning: [unchecked] unchecked conversion
found : java.util.ArrayList
required: java.util.ArrayList<java.lang.String>
shallowCopy = (ArrayList) original.clone();
^
1 warning

Without looping through each element, how do I shallow copy the original
ArrayList?

Thanks!

Gary


 
Reply With Quote
 
 
 
 
Tilman Bohn
Guest
Posts: n/a
 
      12-13-2004
On Mon, 13 Dec 2004 11:26:50 -0700, Gary Newell wrote:

> Without looping through each element, how do I shallow copy the original
> ArrayList?


shallowCopy = new ArrayList<String>( original );

Cheers, Tilman

--
`Boy, life takes a long time to live...' -- Steven Wright

 
Reply With Quote
 
 
 
 
Tilman Bohn
Guest
Posts: n/a
 
      12-13-2004
PS: Also note that you're only getting a compiler warning, not
an error. The code still compiles and works as intended.

Cheers, Tilman

--
`Boy, life takes a long time to live...' -- Steven Wright

 
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