0
我努力學習泛型和迭代器中的Java,並創建下面的代碼 - >泛型,迭代器 - >不能訪問內部類功能
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item>
{
private int N; // number of elements on list
private Node<Item> pre; // sentinel before first item
private Node<Item> post; // sentinel after last item
/*
* linked list node helper data type
*/
private static class Node<Item>
{
private Item item;
private Node<Item> next;
private Node<Item> prev;
}//class Node<Item> ends
/*
*construct an empty deque
*/
public Deque()
{
pre = new Node<Item>();
post = new Node<Item>();
pre.item = null;
post.item = null;
pre.prev = null;
post.next = null;
pre.next = post;
post.prev = pre;
N = 0;
}//Deque ends
/*
* is the deque empty?
*/
public boolean isEmpty()
{
//if(((pre.next == post)&&(post.prev == pre)) || (N == 0) ) return true;
if(N == 0) return true;
else return false;
}//isEmpty ends
/*
* return the number of items on the deque
*/
public int size()
{
return N;
}//size ends
/*
* insert the item at the front
* This is extension of Queue, so First is the location near Post Node
* pre-> <-post, pre->1<-post, pre->1,2<-post, pre->1,2,3<-post ...
* ^ ^ ^^ ^^^
* | | | | | | |
* initial these all are the effects of addFirst()
* condition
*/
public void addFirst(Item item)
{
//System.out.println("We are outside ListIterator - addFirst");
if(isEmpty()) //here pre.next == post.prev
{
//System.out.println("We are inside isEmpty");
Node<Item> NewNode = new Node<Item>();
NewNode.item = item;
NewNode.next = post;
post.prev = NewNode;
NewNode.prev = pre;
pre.next = NewNode;
N++;
//System.out.println(NewNode.item);
}//if ends
else //here pre.next->1.prev & X.next->post
{
//System.out.println("We are inside !isEmpty");
Node<Item> NewNode = new Node<Item>();
Node<Item> last = post.prev;
NewNode.item = item;
NewNode.next = post;
post.prev = NewNode;
NewNode.prev = last;
last.next = NewNode;
N++;
//System.out.println(NewNode.item);
}//else ends
}//addFirst ends
/*
*insert the item at the end
* This is extension of Queue, so First is the location near Post Node
* pre-> <-post, pre->1<-post, pre->2,1<-post, pre->3,2,1<-post ...
* ^ ^ ^^ ^^^
* | | | | | | |
* initial these all are the effects of addLast()
* condition
*/
public void addLast(Item item)
{
//System.out.println("We are outside ListIterator - addLast");
if(isEmpty()) //here pre.next == post.prev
{
//System.out.println("We are inside isEmpty");
Node<Item> NewNode = new Node<Item>();
NewNode.item = item;
NewNode.next = post;
post.prev = NewNode;
NewNode.prev = pre;
pre.next = NewNode;
N++;
System.out.println(NewNode.item);
}//if ends
else //here pre.next->1.prev & X.next->post
{
//System.out.println("We are inside !isEmpty");
Node<Item> NewNode = new Node<Item>();
Node<Item> last = pre.next;
NewNode.item = item;
NewNode.next = last;
pre.next = NewNode;
NewNode.prev = pre;
last.prev = NewNode;
N++;
System.out.println(NewNode.item);
}//else ends
}//addLast ends
/*
* delete and return the item at the front
* This is extension of Queue, so Last is the location near Pre Node
* pre->1,2,3<-post, pre->1,2<-post, pre->1<-post, pre-> <-post...
* ^^^ ^^ ^ ^
* | | | | | | |
* initial these all are the effects of removeFirst()
* condition
*/
public Item removeFirst()
{
if(isEmpty()) throw new NoSuchElementException("Queue underflow EXIT");
else
{
Item item = post.prev.item;
post.prev = post.prev.prev;
N--;
if(isEmpty())
{
post.prev = pre;
pre.next = post;
N = 0;
}//if ends
return item;
}//else ends
}//removeFirst ends
/*
* delete and return the item at the end
* This is extension of Queue, so Last is the location near Pre Node
* pre->1,2,3<-post, pre->2,3<-post, pre->3<-post, pre-> <-post...
* ^^^ ^^ ^ ^
* | | | | | | |
* initial these all are the effects of removeLast()
* condition
*/
public Item removeLast()
{
if(isEmpty()) throw new NoSuchElementException("Queue underflow EXIT");
else
{
Item item = pre.next.item;
pre.next = pre.next.next;
N--;
if(isEmpty())
{
pre.next = post;
post.prev = pre;
N = 0;
}//if ends
return item;
}//else ends
}//removeLast ends
/*
* return an iterator over items in order from front to end
* Returns an iterator that iterates over the items in this queue in FIFO order.
*/
public Iterator<Item> iterator()
{
return new ClassIterator<Item>(pre);
}//Iterator<Item> iterator() ends
// an iterator, doesn't implement remove() since it's optional
private static class ClassIterator<Item> implements Iterator<Item>
{
private Node<Item> current;
private int index = 0;
public ClassIterator(Node<Item> pre)
{
current = pre.next;
index = 1;
//System.out.println(current.item);
}
public boolean hasNext()
{
//System.out.println(current.item);
return current.next != null;
}
public void remove() { throw new UnsupportedOperationException(); }
public Item next()
{
if (!hasNext())
{
System.out.println("Queue is empty!!!");
throw new NoSuchElementException();
}
Item item = current.item;
current = current.next;
index++;
return item;
}
public void DisplayIndex(int indexVal)
{
if(index == indexVal) System.out.println(current.item);
else {}
}//DisplayIndex ends
}//class ListIterator<Item> ends
public String toString()
{
StringBuilder s = new StringBuilder();
for (Item item : this) s.append(item + " ");
return s.toString();
}
/*
* Display at random based on the indices supplied from the main
*/
/*
* main function for unit testing
*/
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
int K = Integer.parseInt(args[1]);
Deque<String> list = new Deque<String>();
System.out.println("Adding to the list by - addFirst");
for (int i = 0; i < N; i++) list.addFirst(StdIn.readString());
StdOut.println(list);
StdOut.println();
Iterator<String> iter = list.iterator();
// print random index value
StdOut.println("Index Value = 1");
boolean a = iter.hasnext();
iter.DisplayIndex(1);
}//main ends
}
的問題是,我不能夠訪問hasnext()
和DisplayIndex
功能與ClassIterator
即使我已創建Iterator
的對象。任何幫助?
我在iter.hasnext()和iter.DisplayIndex(1)函數調用中看到問題。請原諒我,如果我正在做一些天真的錯誤,我是新來的java ... – user3043882
'迭代器'沒有'hasnext()'方法;它有一個'hasNext()'方法。大小寫在Java中很重要。 –
我創建了一個鏈表,我試圖通過迭代器訪問其成員... – user3043882