java - 迭代器和抛出异常
问题描述
我正在尝试制作双端双端队列,但我一直遇到错误,坦率地说我不知道如何解决。第一个是关于双端队列。在我的迭代器函数中,我不断收到以下错误,我不知道为什么:
Deque.java:105: error: incompatible types: Deque.DequeIterator cannot be converted to Iterator<Item>
return new DequeIterator();
此外,我一直在尝试抛出异常,但由于某种原因无法抛出。我不断收到以下错误:
Deque.java:72: error: cannot find symbol
throw java.util.NoSuchElementException();
^
symbol: class util
location: package java
这是我的代码:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item>{
private int size;
private Node<Item> first;
private Node<Item> last;
private class Node<Item>{
Item item;
Node<Item> next;
Node<Item> prev;
Node(Item item) {
this.item = item;
next = null;
prev = null;
}
}
// construct an empty deque
public Deque(){
first = null;
last = null;
size = 0;
}
public boolean isEmpty(){return size == 0;} // is the deque empty?
public int size(){return size;} // return the number of items on the deque
// add the item to the front
public void addFirst(Item item){
if (item == null){
throw new java.lang.NullPointerException();
}
else if (this.isEmpty()){
first = new Node(item);
first = last;
}
else{
Node oldfirst = first;
Node first = new Node(item);
first.next = oldfirst;
oldfirst.prev = first;
}
size ++;
}
// add the item to the end
public void addLast(Item item){
if (item == null){
throw new java.lang.NullPointerException();
}
else if (this.isEmpty()){
Node last = new Node(item);
last = first;
}
else{
Node oldlast = last;
Node last = new Node(item);
oldlast.next = last;
last.prev = oldlast;
}
size ++;
}
// remove and return the item from the front
public Item removeFirst(){
if (this.isEmpty()){
throw java.util.NoSuchElementException();
}
else{
Item item = first.item;
first = first.next;
first.prev = null;
if (size == 1){
first = last;
}
size --;
return item;
}
}
// remove and return the item from the end
public Item removeLast(){
if (this.isEmpty()){
throw java.util.NoSuchElementException();
}
else{
Item item = last.item;
last = last.prev;
if (size == 1){
last = first;
}
size --;
return item;
}
}
// return an iterator over items in order from front to end
public Iterator<Item> iterator() {
return new DequeIterator();
}
private class DequeIterator<Item> implements Iterable<Item>{
private Node current;
public DequeIterator() { this.current = first;}
public boolean hasNext(){ return current != null;};
public void remove() {throw new UnsupportedOperationException();}
public Item next(){
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
// unit testing (optional)
public static void main(String[] args){
Deque<String> deque = new Deque<String>();
deque.addFirst("1");
//StdOut.println("addfirst to string: " + deque.AXCToString());
deque.addFirst("2");
//StdOut.println("addfirst to string: " + deque.AXCToString());
deque.addFirst("3");
//StdOut.println("addfirst to string: " + deque.AXCToString());
deque.addFirst("4");
//StdOut.println("addfirst to string: " + deque.AXCToString());
deque.addFirst("5");
}
}
解决方案
你的第一个问题是你的DequeIterator
类实现Iterable
,什么时候应该实现Iterator
。Iterable
通常用于集合之类的东西,然后可以提供一个Iterator
实例。看起来您已经实现了 的方法Iterator
,所以这应该只是将行更改为:
private class DequeIterator<Item> implements Iterator<Item> {
对于第二个问题,您缺少new
构造异常的关键字。它应该如下:
throw new java.util.NoSuchElementException();
此外,Java 中的标准做法是使用导入而不是使用绝对路径,并且看起来您已经导入了它,因此您可以将其缩短为:
throw new NoSuchElementException();
推荐阅读
- flutter - Flutter:Textformfield验证器不起作用
- regex - 正则表达式:如果存在关键字,则匹配所有数字
- javascript - 为什么定义了这么多 Javascript 变量但在网页中没有使用?
- javascript - setInterval 函数不执行
- swift - SwiftUI 和 RxSwift 观察者关闭行为
- excel - MOUSEEVENTF_LEFTDOWN 有时不点击
- python - 在 Pandas 中将数据帧写入 CSV 时保持精度
- java - 如何在 Java 中使用 Microsoft Bot Framework 呈现 markdown?
- c# - EF Core - 从 SQL 视图加载的导航属性
- python - 序列化不同类型的 MATLAB 数据结构以发送到 Python