gpt4 book ai didi

Java Deque 实现无法转换 Item

转载 作者:行者123 更新时间:2023-12-02 09:08:25 32 4
gpt4 key购买 nike

双端队列实现我实现了一个通用的 Deque 数据结构。

请检查此实现

这个错误对我来说没有意义,请告诉我一点信息

import java.util.NoSuchElementException;
import java.util.Iterator;
public class Deque<Item> implements Iterable<Item>
{
private int n;
private Node first;
private Node last;
private class Node
{
private Item item;
private Node next;
}
private class ListIterator implements Iterable<Item>
{
private Node<Item> current = first;
public boolean hasNext()
{
return current != null;
}
public Item next()
{
if(!hasNext())
{
throw new NoSuchElementException("Deque underflow");
}
Item item = current.item;
current = current.next;
return item;
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
public Iterator<Item> iterator()
{
return new ListIterator();
}
public Deque()
{
n = 0;
first = null;
last = null;
}

// is the deque empty?
public boolean isEmpty()
{
return n ==0;
}

// return the number of items on the deque
public int size()
{
return n ;
}

// add the item to the front
public void addFirst(Item item)
{
if(item == null)
{
throw new IllegalArgumentException();
}
Node oldNode = first;
Node newNode = new Node();
newNode.item = item;
if(oldNode == null)
{
last = newNode;
}
else
{
newNode.next = oldNode;
}
first = newNode;
n++;
}

// add the item to the back
public void addLast(Item item)
{
if(item == null)
{
throw new IllegalArgumentException();
}
Node oldNode = last;
Node newNode = new Node();
newNode.item = item;
if(oldNode == null)
{
first = newNode;
}
else
{
oldNode.next = newNode;
}
last = newNode;
n++;
}

// remove and return the item from the front
public Item removeFirst()
{
if(isEmpty())
{
throw new NoSuchElementException("Deque underflow");
}
Item item = first.item;
first = first.next;
n--;
if(isEmpty())
{
first=null;
}
return item;
}

// remove and return the item from the back
public Item removeLast()
{
if(isEmpty())
{
throw new NoSuchElementException("Deque underflow");
}
Node secondLast = first;
if(n>3)
{
while(secondLast.next.next != null)
{
secondLast = secondLast.next;
}
}
else
{
secondLast = first;
}
Item item = last.item;
last = secondLast;
n--;
if(isEmpty())
{
last = null;
}
return item;
}
}

测试文件

import java.util.Iterator;

public class dequeTest
{
public static void main(String[] args)
{
Deque<Double> d = new Deque<Double>();
System.out.println("Empty? "+d.isEmpty()+" \tSize is "+ d.size());
d.addFirst(2.0);
System.out.println(d.removeFirst());
System.out.println("Empty? "+d.isEmpty()+" \tSize is "+ d.size());
d.addFirst(1.2);
System.out.println(d.removeLast());
System.out.println("Empty? "+d.isEmpty()+" \tSize is "+ d.size());
d.addLast(1.4);
System.out.println("Empty? "+d.isEmpty()+" \tSize is "+ d.size());
for(int i = 0;i<10;i++)
{
d.addLast((double)i);
}
for(double value: d)
{
System.out.print(value);
}
}
}

错误类型不匹配:无法从 Deque.ListIterator 转换为 Iterator

谁能帮忙解决这个错误吗?如果您有更好的想法,请修复它

最佳答案

Iterable<T> 不是 Iterator<T> ,但它有一个方法来获取 Iterator<T> 。只需更改

public Iterator<Item> iterator()
{
return new ListIterator();
}

public Iterator<Item> iterator()
{
return new ListIterator().iterator();
}

除此之外,我建议您使用 ArrayDeque (甚至 LinkedList )当您需要 Deque 时。您的实现似乎没有改进,更糟糕的是您的类名遮盖了 java.util.Deque 接口(interface)(您不实现)。最后,一个Deque应该是Collection (不仅仅是Iterable)。

关于Java Deque 实现无法转换 Item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606900/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com