このコードは教科書「Java by Dissection」で見ましたが、正確には何をしているのかわかりません。教科書は、ネストされたクラスを実装していると言う以外に、このコードについてほとんど何も説明していません。しかし、私はそれが実際に何をするのかを理解したいと思っています。
このコードを理解したい理由は、値が 1 ~ 10 の MyList クラスのオブジェクトを宣言/インスタンス化するメインを作成しようとしていたからです。次に、上部にいくつかの数字を追加し、必要な場所からいくつかを削除します. 誰でもこれで私を助けることができますか?
私が理解していない主な部分は、ネストされたクラス - ListedElement です。
public class MyList {
private ListElement head, tail; //Forward declaration
void add(Object value) {
if (tail != null) {
tail.next = new ListElement(value);
tail = tail.next;
}
else {
head = tail = new ListElement(value);
}
}
Object remove()
{
assert head != null; // don't remove on empty list
Object result = head.value;
head = head.next;
if (head == null) { //was that the last?
tail = null;
}
return result;
}
//Nested class needed only in the implementation of MyList
private class ListElement {
ListElement(Object value) {this.value = value;}
Object value;
ListElement next; //defaults to null as desired
}
}