之前初学时看过可以实现Iterable接口实现Iterator迭代器的支持,并且也支持foreach循环。现在学习了数据结构,手动写一个单链表支持foreach循环吧。

手写foreach循环步骤:

1.实现Iteratable接口

2.内部类实现Iterator接口,重写getNext方法和next方法

例子:写了这两步即可实现foreach循环和Iterator迭代

package com.littlepage.foreach;

public class Node<T> {
//member variable
private T data;
private Node<T> next; //constructor
public Node() {
data=null;
next=null;
} public Node(T t) {
data=t;
} //getters and setters
public T getData() {
return data;
} public void setData(T data) {
this.data = data;
} public Node<T> getNext() {
return next;
} public void setNext(Node<T> next) {
this.next = next;
} //toString
@Override
public String toString() {
return data.toString();
} //equals
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
Node<T> temp1=this;
Node<T> node=(Node<T>)obj;
if(!data.equals(node.getData())) return false;
else if(next==null&&node.getNext()!=null) return false;
else if(next!=null&&node.getNext()==null) return false;
else if(next==null&&node.getNext()==null) return true;
else{
temp1=temp1.getNext();
node=node.getNext();
return temp1.equals(node);
}
}
}
package com.littlepage.foreach;

import java.util.Iterator;

public class SinglyLinkedList<T> implements Iterable<T> {
//member variable
private Node<T> head;
private int size; //constructor
public SinglyLinkedList() {
head=new Node<>();
} public SinglyLinkedList(T[] element) {
head=new Node<T>();
for(int i=;i<element.length;i++){
if(head.getData()==null) head.setData(element[i]);
else {
Node<T> temp=head;
while(temp.getNext()!=null){
temp=temp.getNext();
}
temp.setNext(new Node<T>(element[i]));
}
}
size=element.length;
} public SinglyLinkedList(SinglyLinkedList<T> singlyLinkedList){
head=singlyLinkedList.getHead();
size=singlyLinkedList.getSize();
} //getters and setters
public Node<T> getHead() {
return head;
} public void setHead(Node<T> head) {
this.head = head;
} public int getSize() {
return size;
} public void setSize(int size) {
this.size = size;
} //toString
@Override
public String toString() {
StringBuilder sb=new StringBuilder("[");
sb.append(head.getData()+",");
Node<T> temp=head;
while(temp.getNext()!=null){
temp=temp.getNext();
sb.append(temp.getData()+",");
}
sb.replace(sb.lastIndexOf(","), sb.lastIndexOf(",")+, "]");
return sb.toString();
} //others method
public void concat(SinglyLinkedList<T> list){
Node<T> temp=head;
while(temp.getNext()!=null){
temp=temp.getNext();
}
temp.setNext(list.getHead());
size+=list.getSize();
} public Node<T> search(T element){
Node<T> temp=head;
if(head.getData().equals(element)) return head;
while(temp.getNext()!=null){
if(temp.getNext().getData().equals(element)) return temp.getNext();
temp=temp.getNext();
}
return null;
} public boolean contain (T element){
Node<T> temp=head;
if(head.getData().equals(element)) return true;
while(temp.getNext()!=null){
if(temp.getNext().getData().equals(element)) return true;
temp=temp.getNext();
}
return false;
} public void add(T element){
if(head.getData()==null) head.setData(element);
else{
if(head.getNext()==null) head.setNext(new Node<>(element));
else{
Node<T> temp=head;
while(temp.getNext()!=null){
temp=temp.getNext();
}
temp.setNext(new Node<>(element));
}
}
size++;
} public boolean remove (T element){
Node<T> temp=head;
if(head.getData().equals(element)) {
head=head.getNext();
size--;
return true;
}
while(temp.getNext()!=null){
if(temp.getNext().getData().equals(element)) {
temp.setNext(temp.getNext().getNext());
return true;
}
temp=temp.getNext();
}
return false;
} public boolean replace (Object obj, T element){
Node<T> temp=head;
if(head.getData().equals(obj)) {
head.setData(element);
return true;
}
while(temp.getNext()!=null){
if(temp.getNext().getData().equals(obj)) {
temp.getNext().setData(element);
return true;
}
temp=temp.getNext();
}
return false;
} //getElement
public T getElement(int index){
if(index<||index>=size) return null;
Node<T> temp=head;
if(index==) return head.getData();
else{
for(int i=;i<index;i++){
temp=temp.getNext();
}
return temp.getData();
}
} //getNode
public Node<T> getNode(int index){
if(index<||index>=size) return null;
Node<T> temp=head;
if(index==) return head;
else{
for(int i=;i<index;i++){
temp=temp.getNext();
}
return temp;
}
} public void set(int index,T value){
if(index<) index=;
if(index>=size) index=size-;
Node<T> temp=head;
if(index==) head.setData(value);
else{
for(int i=;i<index;i++){
temp=temp.getNext();
}
temp.setData(value);
}
} @Override
public boolean equals(Object obj) {
@SuppressWarnings("unchecked")
SinglyLinkedList<T> singlyLinkedList=(SinglyLinkedList<T>)obj;
return head.equals(singlyLinkedList.getHead());
} @Override
public Iterator iterator() {
class MyIterator implements Iterator{
Node temp=head;
@Override
public boolean hasNext() {
return temp!=null;
} @Override
public Object next() {
Node<T> pre=temp;
temp=temp.getNext();
return pre.getData();
}
}
return new MyIterator();
}
}
package com.littlepage.foreach;

import java.util.LinkedList;

public class Test {
public static void main(String[] args) {
SinglyLinkedList<Integer> sll=new SinglyLinkedList<>();
sll.add(5);
sll.add(4);
for (Integer e:sll) {
System.out.println(e);
}
}
}

  

05-11 20:13