私のプログラムでは、独自の LinkedList クラスを作成します。そしてインスタンス、llist。
次のように foreach ループで使用するには、LinkedList に Iterable を実装する必要がありますか?
for(Node node : llist) {
System.out.print(node.getData() + " ");
}
以下は、私の LinkedList クラスです。Iterable にする方法を教えてください。
public class LinkedList implements Iterable {
private Node head = null;
private int length = 0;
public LinkedList() {
this.head = null;
this.length = 0;
}
LinkedList (Node head) {
this.head = head;
this.length = 1;
}
LinkedList (LinkedList ll) {
this.head = ll.getHead();
this.length = ll.getLength();
}
public void appendToTail(int d) {
...
}
public void appendToTail(Node node) {
...
}
public void deleteOne(int d) {
...
}
public void deleteAll(int d){
...
}
public void display() {
...
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public boolean isEmpty() {
if(this.length == 0)
return true;
return false;
}
}