Я написал здесь метод add (Comparable obj): однако, когда я запускаю на нем тест, такой как приведенный ниже, напечатанный список состоит только из самого последнего добавленного элемента. Я не могу найти разрыв в моей логике c.
Когда я пытаюсь увеличить размер ++ перед тем, как l oop, я получаю "Исключение в потоке" main "java .lang.ClassCastException: class java .lang.Character не может быть приведен к class java .lang.String (java .lang.Character и java .lang.String находятся в модуле java .base загрузчика 'bootstrap') ".
РЕДАКТИРОВАТЬ: Полный код:
/**
* Class OrderedLinkedList.
* <p>
* This class functions as a linked list, but ensures items are stored in ascending order.
*/
public class OrderedLinkedList {
/**************************************************************************
* Constants
*************************************************************************/
/**
* return value for unsuccessful searches
*/
private static final OrderedListNode NOT_FOUND = null;
/**************************************************************************
* Attributes
*************************************************************************/
/**
* current number of items in list
*/
private int theSize;
/**
* reference to list header node
*/
private OrderedListNode head;
/**
* reference to list tail node
*/
private OrderedListNode tail;
/**
* current number of modifications to list
*/
private int modCount;
/**************************************************************************
* Constructors
*************************************************************************/
/**
* Create an instance of OrderedLinkedList.
*/
public OrderedLinkedList() {
// empty this OrderedLinkedList
clear();
}
/**************************************************************************
* Methods
*************************************************************************/
/*
* Add the specified item to this OrderedLinkedList.
*
* @param obj the item to be added
*/
public boolean add(Comparable obj) {
// TODO: Implement this method (8 points)
OrderedListNode node = new OrderedListNode(obj, null, null);
//if list is empty, add to beginning of list
if (isEmpty()) {
head.next = node;
node.next = tail;
tail.previous = node;
}
//if theItem is less than the first element, add it to the beginning of the list
if (obj.compareTo(head.next.theItem) < 0) {
node.next = head.next;
head.next = node;
}
OrderedListNode pointer = head.next;
while (pointer.theItem.compareTo(obj) < 0 && pointer.next != null) {
pointer = pointer.next;
node.previous = pointer.previous;
node.next = pointer;
pointer.previous.next = node;
pointer.previous = node;
theSize++;
modCount++;
}
return true;
}
/*
* Remove the first occurrence of the specified item from this OrderedLinkedList.
*
* @param obj the item to be removed
*/
public boolean remove(Comparable obj) {
// TODO: implement this method (7 points)
OrderedListNode node = head.next;
if (isEmpty()) {
return false;
}
while (node != null) {
if (obj.compareTo(node) == 0) {
node.next.previous = node.previous;
node = node.next;
return true;
}
node = node.next;
}
++modCount;
return false;
}
/**
* Empty this OrderedLinkedList.
*/
public void clear() {
// reset header node
head = new OrderedListNode("HEAD", null, null);
// reset tail node
tail = new OrderedListNode("TAIL", head, null);
// header references tail in an empty LinkedList
head.next = tail;
// reset size to 0
theSize = 0;
// emptying list counts as a modification
modCount++;
}
/**
* Return true if this OrderedLinkedList contains 0 items.
*/
public boolean isEmpty() {
return theSize == 0;
}
/**
* Return the number of items in this OrderedLinkedList.
*/
public int size() {
return theSize;
}
/*
* Return a String representation of this OrderedLinkedList.
*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String s = "";
OrderedListNode currentNode = head.next;
while (currentNode != tail) {
s += currentNode.theItem.toString();
if (currentNode.next != tail) {
s += ", ";
}
currentNode = currentNode.next;
}
return s;
}
/**************************************************************************
* Inner Classes
*************************************************************************/
/**
* Nested class OrderedListNode.
* <p>
* Encapsulates the fundamental building block of an OrderedLinkedList
* contains a data item, and references to both the next and previous nodes
* in the list
*/
public static class OrderedListNode {
Comparable theItem;
OrderedListNode next;
OrderedListNode previous;
OrderedListNode(Comparable item, OrderedListNode prev, OrderedListNode next) {
this.theItem = item;
this.previous = prev;
this.next = next;
}
}
// TODO: Implement the nested class OrderedListNode (5 points). This nested class
// should be similar to the nested class ListNode of the class LinkedList, but
// should store a data item of type Comparable rather than Object.
public static void main(String[] args) { System.out.println("test");
OrderedLinkedList listOne = new OrderedLinkedList();
listOne.add('1');
listOne.add('2');
listOne.add('3');
System.out.println(listOne + " ");
}
}