java - 袋子 remove() 方法
问题描述
我被赋予了这样的包课
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Bag<Item> implements Iterable<Item> {
private int N; // number of elements in bag
private Node<Item> first; // beginning of bag
// helper linked list class
private class Node<Item> {
private Item item;
private Node<Item> next;
}
/**
* Initializes an empty bag.
*/
public Bag() {
first = null;
N = 0;
}
/**
* Is this bag empty?
* @return true if this bag is empty; false otherwise
*/
public boolean isEmpty() {
return first == null;
}
/**
* Returns the number of items in this bag.
* @return the number of items in this bag
*/
public int size() {
return N;
}
/**
* Adds the item to this bag.
* @param item the item to add to this bag
*/
public void add(Item item) {
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldfirst;
n++;
}
public void remove(Item item){
// currentNode is the reference to the first node in the list and to the Item
Node<Item> currentNode = first;
// if items equals the first node in the list, then first = currentNode.next which will make the first item
Node<Item> temp = currentNode;
while(temp.next != null){
temp = currentNode;
if(item.equals(currentNode.item)){
currentNode = currentNode.next;
temp.next = currentNode;
break;
}else{
currentNode = currentNode.next;
}
}
N--;
}
/**
* Returns an iterator that iterates over the items in the bag in arbitrary order.
* @return an iterator that iterates over the items in the bag in arbitrary order
*/
public ListIterator<Item> iterator() {
return new ListIterator<Item>(first);
}
// an iterator, doesn't implement remove() since it's optional
private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> current;
public ListIterator(Node<Item> first) {
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;
}
}
可以看出,remove() 方法是可选的,因此它不包含算法。
我写了这样的删除方法:
public void remove(Item item) {
Node<Item> currentNode = (Node<Item>) first;
Node<Item> previousNode = null;
while(currentNode != null){
if(item.equals(currentNode.item)){
if(previousNode == null) {
first = (Node<Item>) currentNode.next;
}
else {
previousNode.next = currentNode.next;
}
n--;
}
else {
previousNode = currentNode;
}
currentNode = currentNode.next;
}
}
然而,
first = (Node<Item>) currentNode.next;
这一行给出了“类型不匹配:无法从 Bag.Node 转换为 Bag.Node”错误,这让我感到困惑。
应该做些什么来克服这个错误,或者我的删除方法中是否缺少部分?
解决方案
public class Bag<Item> implements Iterable<Item> {
// ...
private class Node<Item> {
// ...
}
// ...
}
这定义了两个不同的类型变量,都具有 name Item
。
如果您希望 的实例Node
使用与 的包含实例相同的类型变量Bag
,请删除<Item>
on Node
:
private class Node {
推荐阅读
- kubernetes - 从 hashcorp vault 填充 kubernetes Configmap
- java - Android Pie中的图像从画廊上传到服务器的问题
- api - 在 GraphQL 中处理数据模式中的特殊字符
- python - Django 我如何追溯此调用的完成位置
- python - 熊猫计算满足条件的行的列的平均值
- django - Django unitTest response.content 不显示正确的用户信息
- imap - Exchange Basic Auth 停用后,我们如何处理服务帐户?
- linq - 将表的 id 与子查询中的最大 id 左连接
- ruby-on-rails - 无法上传 xls 文件,但可以通过 Shrine Uploader 上传 xlsx 文件
- gradle - 如何从 web 应用程序或任何 java API 发送 gradle 任务指令