I am creating a generic class for a doubly-linked circular list with a dummy header node.
After searching StackOverflow, I have changed my class declarations (The list and the node) to extend Comparable and Cloneable as such:
public class DoublyLinkedList <T extends Comparable & Cloneable>{
private static class Node<T extends Comparable & Cloneable>
...
However, this line of code:
Node trav = new Node(l.current.data.clone());
Gives me this error:
DoublyLinkedList.java:43: error: clone() has protected access in Object
Node trav = new Node((T)l.current.data.clone());
^
I have changed it to this:
Node trav = new Node((T)l.current.data.clone());
and the error remains. What am I missing? Thanks in advance.
Note: the Node constructor I'm calling looks like this:
private Node (T d)
{
data=d;
prev=null;
next=null;
}
Edit: Here is where the problematic code lies:
public DoublyLinkedList(DoublyLinkedList<T> l)
{
size = l.size();
this.head = new Node<T>(null);
current = this.head;
l.begin();
while (!l.end()) {
Node trav = new Node(((T) l.current.data).clone());
current.next = trav;
trav.prev = current;
current = trav;
l.advance();
}
current.next = this.head;
this.head.prev = current;
}
Final Edit: Thanks to RealSkeptic, I have learned that two references to the same object makes no difference for immutable objects and shouldn't be the concern of a generic container for mutable objects, therefore I have decided to go with the following code:
Node trav = new Node(l.current.data);