質問

I am trying to create a clone method and copy constructor that create deep copies of a Doubly Linked list but I am getting a few compiler errors that I just can't seem to figure out. Particularly in the copyOf method, which I am using to assist with the creation of deep copies, for the line:

newHead = new TwoWayNode<T>((T)(position.data).clone(), null, null);

The compiler is saying that clone() has protected access in Object. The second issue is coming from this line:

end.next = new TwoWayNode<T>((T)(position.data).clone(), head, null));

For this error I am getting the same message as above, but I am also seeing that there should be a ';' expected, which isn't making sense to me. Anyone have any insight about what I might be doing wrong?

public DoublyLinkedList(DoublyLinkedList<T> otherList)
{
    if (otherList == null)
        throw new NullPointerException();
    if (otherList.head == null)
        head = null;
    else
        head = copyOf(otherList.head);
}

public DoublyLinkedList<T> clone()
{
    try
    {
        DoublyLinkedList<T> copy = (DoublyLinkedList<T>)super.clone();
        if (head == null)
            copy.head = null;
        else 
            copy.head = copyOf(head);
        return copy;
    }
    catch(CloneNotSupportedException e) //should not happen
    {
        return null;
    }
}

private TwoWayNode<T> copyOf(TwoWayNode<T> otherHead)
{
    TwoWayNode<T> position = otherHead;
    TwoWayNode<T> newHead; 
    TwoWayNode<T> end = null;

    try
    {
        newHead = new TwoWayNode<T>((T)(position.data).clone(), null, null);
        end = newHead;
        position = position.next;
        while (position != null)
        {
            end.next = new TwoWayNode<T>((T)(position.data).clone(), head, null);
            end = end.next;
            position = position.next;
        }
        return newHead;
    }
    catch(CloneNotSupportedException e)
    {
        return null;
    }
}

Edit The parenthesis issue has been corrected, but I am still having trouble with the protected access for clone(). The portion of my demo file bringing the error looks like this right now:

    System.out.println("Initializing copy constructor");
    DoublyLinkedList list2 = new DoublyLinkedList(list1);
    DoublyLinkedList.DoublyLinkedIterator j = list2.iterator();
    System.out.println("list2 contains:");

    j.restart();
    while (j.hasNext())
        System.out.println(j.next());
    System.out.println("");
役に立ちましたか?

解決

You have more closing parenthesis, so ; issue will be fixed if you will delete one.

end.next = new TwoWayNode<T>((T)(position.data).clone(), head, null);

For the other one, just replace this line

newHead = new TwoWayNode<T>((T)(position.data).clone(), null, null);

with this one.

newHead = new TwoWayNode<T>((T)((T)position.data).clone(), null, null);

This will prevent it to access the Object class's clone() method, and will use your class's version (i.e. implemented by you) of clone()

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top