shallow copying an ArrayList in Java 5

Discussion in 'Java' started by Gary Newell, Dec 13, 2004.

  1. Gary Newell

    Gary Newell Guest

    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
     
    Gary Newell, Dec 13, 2004
    #1
    1. Advertising

  2. Gary Newell

    Tilman Bohn Guest

    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
     
    Tilman Bohn, Dec 13, 2004
    #2
    1. Advertising

  3. Gary Newell

    Tilman Bohn Guest

    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
     
    Tilman Bohn, Dec 13, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,794
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,484
    Kaidi
    Jan 3, 2004
  3. Ike

    Copying into ArrayList

    Ike, Sep 2, 2006, in forum: Java
    Replies:
    2
    Views:
    7,072
    Ralf Seitner
    Sep 2, 2006
  4. xz
    Replies:
    16
    Views:
    2,441
  5. Philipp
    Replies:
    6
    Views:
    968
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page