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; }