0

つまり、カスタムLinkedListというタスクを実行しています。MyLinkedList のディープ コピーを作成するために Object.clone をオーバーライドする必要がありますが、わかりません。mested クラスと外部クラスの両方を複製する必要がありますか? コードは次のとおりです。

public class MyLinkedList implements Cloneable {

    private Entry header;
    private int size;

    .....................

    @Override
    public Object clone() {
        MyLinkedList newObject = null;
        try {
            newObject = (MyLinkedList) super.clone();
            newObject.header = this.header.clone();
        } catch (CloneNotSupportedException e) {
            System.err.print("Clone not supported");
        }
        return newObject;
    }

    private static class Entry implements Cloneable {
        double data;
        Entry prev;
        Entry next;

        public Entry(double data, Entry next, Entry prev) {
            this.data = data;
            this.next = next;
            this.prev = prev;    
        }

        @Override
        public Entry clone() {
            Entry newObject = null;
            try {
                newObject = (Entry) super.clone();
                newObject.prev = this.prev.clone();
                newObject.next = this.next.clone();
            } catch (CloneNotSupportedException e) {
                System.err.print("Clone not supported");
            }
            return newObject;
        }
    }   
}

MyLinkedList のクローンを作成しようとすると、java.lang.StackOverflowError が発生します。私は何を間違っていますか?

4

1 に答える 1