Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Writing clone() method for LinkedList

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Writing clone() method for LinkedList

    Hello, JPF. I am trying to implement my own LinkedList and the clone method I have written doesn't seem to work. Can anyone tell me what I did wrong? Thanks!

    Okay, this is what I have in my tester:
    import java.util.*;
    public class OurLinkedListTest
    {  public static void main (String [] args)
       {  try
         {
          OurLinkedList <String> myList = new OurLinkedList<String>();
          myList.add("A");
          myList.add("B");
          myList.add("C");
          myList.add("B");
          myList.add(2, "X");
          myList.addFirst("Y");
          myList.addLast("Z");
          OurLinkedList <String>cloned = (OurLinkedList<String>)myList.clone();
       }
       catch(IllegalStateException e)
       {  System.out.println("Rule #2 is violated.");
       } 
    }
    }

    public Object clone()
      {
         LinkedList<String> clone = new LinkedList<String>();
         for (int i=0; i<size; i++)
         {
            clone.add(get(i));
         }
         return clone;
      }
    Last edited by vluong; October 26th, 2009 at 07:05 PM.


  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Writing clone() method for LinkedList

    Why does your clone method repeatedly (assuming size is greater than 1) add "A" into the cloned list and not the values of the list it is cloning?

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Writing clone() method for LinkedList

    literallyjer, the "A" are just used for testing and I forgot to change it before posting (it's been changed). I get this error:

    Exception in thread "main" java.lang.ClassCastException: java.util.LinkedList
    at OurLinkedListTest.main(OurLinkedListTest.java:14)

  4. #4
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Writing clone() method for LinkedList

    I am assuming that line 14 is this:

    OurLinkedList <String>cloned = (OurLinkedList<String>)myList.clone();

    If that's the case, then you are trying to cast a LinkedList into your OurLinkedList. Look closer at your clone method. You are cloning an OurLinkedList into a LinkedList and then trying to cast the LinkedList into an OurLinkedList.

  5. The Following User Says Thank You to literallyjer For This Useful Post:

    vluong (October 26th, 2009)

  6. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Writing clone() method for LinkedList

    Thanks a bunch, literallyjer! It works now!

  7. #6
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Writing clone() method for LinkedList

    You are very welcome :-)

  8. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Writing clone() method for LinkedList

    You should also implement Cloneable.

    // Json

Similar Threads

  1. Writing java applications for the iPod Touch
    By JavaPF in forum Java ME (Mobile Edition)
    Replies: 8
    Last Post: June 14th, 2011, 04:34 PM
  2. How to start writing Java Apps for Cell Phones?
    By Jchang504 in forum Java ME (Mobile Edition)
    Replies: 7
    Last Post: January 10th, 2011, 05:11 AM
  3. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM
  4. Any way to map method calls?
    By Swiftslide in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2009, 04:37 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM