[英]Circular linked list and iterator
I'm having trouble with getting my iterator to actually work and test out my add and next method. 我在让迭代器实际工作并测试我的add和next方法方面遇到麻烦。 I was wondering if anyone can point out what i am missing. 我想知道是否有人可以指出我所缺少的。 I been working at it for awhile and can't seem to figure out how to initialize the object correctly, kept saying cannot resolve to a type. 我花了一段时间,似乎无法弄清楚如何正确地初始化对象,一直说不能解析为一个类型。 Thank You this is my main class 谢谢,这是我的主要课程
public class CircularList<E> {
private int size = 0;
//Inner Class
private static class Node<E>{
private E data;
private Node<E> next=null;
private Node<E> previous=null;
private Node(E dataItem) {
data = dataItem;
}
}
//Iterator
public class CircleIter implements ListIterator<E>{
private Node<E> nextItem;
private Node<E> lastItemReturned;
private int index = 0;
public CircleIter(){
nextItem=null;
lastItemReturned=null;
}
@Override
public void add(E data) {
Node<E> newNode = new Node<E>(data);
if(index==0){
newNode.next=newNode;
newNode.previous=newNode;
nextItem=newNode;
lastItemReturned=newNode;
}
else{
newNode.previous=nextItem.previous;
nextItem.previous.next=newNode;
newNode.next=nextItem;
nextItem.previous=newNode;
}
index++;
size++;
}
@Override
public boolean hasNext() {
return nextItem != null;
}
@Override
public boolean hasPrevious() {
return (lastItemReturned.next == null && size != 0)
|| lastItemReturned.previous != null;
}
@Override //need to look over
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
lastItemReturned = nextItem;
nextItem = nextItem.next;
index++;
return lastItemReturned.data;
}
@Override
public int nextIndex() {
return index;
}
@Override //need to look over
public E previous() {
nextItem=lastItemReturned;
lastItemReturned=lastItemReturned.previous;
index--;
return nextItem.data;
}
@Override
public int previousIndex() {
return index-1;
}
//remove last item returned through next&previous method
@Override
public void remove() {
if (lastItemReturned == null) {
throw new IllegalStateException();
}
nextItem.previous=lastItemReturned.previous;
lastItemReturned.previous.next=nextItem;
index--;
size--;
lastItemReturned = nextItem.previous;
}
@Override
public void set(E node) {
if (lastItemReturned == null) {
throw new IllegalStateException();
}
lastItemReturned.data = node;
}
} //end of iterator class
}
My Test Class 我的考试班
import CircularList.CircleIter;
public class CircularListTest {
public static void main(String[] args) {
CircularList<Integer> intList = new CircularList<Integer>();
CircleIter<Integer> iter= intList.CircleIter<Integer>();
iter.add(5);
}
}
If you want to create an instance of CircularList.CircleIter
class you should use operator "new". 如果要创建CircularList.CircleIter
类的实例,则应使用运算符“ new”。
CircularList.CircleIter
is inner class (non-static nested class) of CircularList class so in order to create new instance of it you should provide instance of enclosing class with special syntax : CircularList.CircleIter
是CircularList.CircleIter
类的内部类(非静态嵌套类),因此,为了创建它的新实例,您应该使用特殊的语法提供封闭类的实例:
CircularList<Integer>.CircleIter iter = intList.new CircleIter();
Usually collections expose special method for iterator creating (for example, iterator() method in List) to hide this syntax from consumers. 通常,集合会公开用于创建迭代器的特殊方法(例如,List中的iterator()方法),以向使用者隐藏此语法。
As Alex Sou mentioned earlier, only enclosing class is parametrized in your difinition. 正如Alex Sou先前提到的,在您的定义中仅参数化了封闭类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.