在类头中,我被要求实现一个名为 AddOnce 的接口。提供了接口,但是 IDE 似乎对与通用 E 相关的任何内容都存在问题。如何修复标头?迭代器尚未完成,但如果可能的话,我想一次迈出一步。
import java.util.Iterator;
/**
*
* @author User
*/
/**
* @param <E> The class of the items in the ordered list
*/
interface AddOnce <E extends Comparable<? super E>> {
/**
* This method searches the list for a previously added
* object, if an object is found that is equal (according to the
* compareTo method) to the current object, the object that was
* already in the list is returned, if not the new object is
* added, in order, to the list.
*
* @param an object to search for and add if not already present
*
* @return either item or an equal object that is already on the
* list
*/
public E addOnce(E item);
}
//generic linked list
public class OrderedAddOnce<E> implements Iterable, AddOnce<E> {
private Node firstNode;
public OrderedAddOnce(){
this.firstNode = null;
}
public E addOnce(E item){
Node current;
//if empty or item is less than head, make item new head
if (firstNode == null || firstNode.data >= item.data) {
item.next = firstNode;
firstNode = item;
return firstNode;
}
//if not empty or item greater than
else {
/* Locate the node before point of insertion. */
current = firstNode;
//while has next
//item greater than node data
//exits once at the end
//or when item <node data
while (current.next != null && current.next.data < item.data) {
current = current.next;
}
item.next = current.next;
current.next = item;
return current.next;
}
}
public Iterator iterator(){
return new AddOnceIterator();
}
}
我已经尝试过标题
public class OrderedAddOnce E implements Iterable, AddOnce<E> {
也
public class OrderedAddOnce<E> implements Iterable, AddOnce<E> {
第一个弹出的错误消息较少,但第二次尝试是我认为我应该更接近的。 我正在尝试将 OrderedAddOnce 实现为通用链表类
选项1: 要修复 OrderedAddOnce 类的标头,您需要使用正确的泛型类型 E 正确实现 Iterable 和 AddOnce 接口。这是您的类的正确标头:
public class OrderedAddOnce<E extends Comparable<? super E>> implements Iterable<E>, AddOnce<E> {
选项2: 我已经完全重写了您的代码,如下所示:
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Represents a generic ordered list that ensures each element is added only once.
*
* @param <E> The type of elements in the ordered list, which must extend Comparable to support comparison.
*/
public class OrderedAddOnce<E extends Comparable<? super E>> implements Iterable<E>, AddOnce<E> {
private Node<E> firstNode;
/**
* Constructs an empty OrderedAddOnce list.
*/
public OrderedAddOnce() {
this.firstNode = null;
}
/**
* Adds the specified item to the ordered list if it is not already present.
* If an equal item is found in the list, the existing item is returned.
*
* @param item The item to be added to the list.
* @return Either the added item or an equal item already present in the list.
*/
public E addOnce(E item) {
Node<E> current;
// Create a new node with the given item
Node<E> newNode = new Node<>(item);
// If the list is empty or the item should be inserted at the beginning
if (firstNode == null || item.compareTo(firstNode.data) <= 0) {
newNode.next = firstNode;
firstNode = newNode;
return firstNode.data;
}
// Otherwise, find the appropriate position to insert the item
current = firstNode;
while (current.next != null && item.compareTo(current.next.data) > 0) {
current = current.next;
}
// Insert the item after the current node
newNode.next = current.next;
current.next = newNode;
return newNode.data;
}
/**
* Returns an iterator over the elements in this ordered list.
*
* @return An iterator over the elements in this ordered list.
*/
public Iterator<E> iterator() {
return new AddOnceIterator();
}
// Private inner class representing an iterator for the OrderedAddOnce list
private class AddOnceIterator implements Iterator<E> {
private Node<E> currentNode = firstNode;
/**
* Returns {@code true} if the iteration has more elements.
*
* @return {@code true} if the iteration has more elements, {@code false} otherwise.
*/
public boolean hasNext() {
return currentNode != null;
}
/**
* Returns the next element in the iteration.
*
* @return The next element in the iteration.
* @throws NoSuchElementException if the iteration has no more elements.
*/
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
E data = currentNode.data;
currentNode = currentNode.next;
return data;
}
}
// Private static inner class representing a node in the linked list
private static class Node<E> {
E data;
Node<E> next;
/**
* Constructs a node with the specified data.
*
* @param data The data to be stored in the node.
*/
public Node(E data) {
this.data = data;
this.next = null;
}
}
}