I need help with the following Java code (the add method) For some reason when I
ID: 3906429 • Letter: I
Question
I need help with the following Java code (the add method)
For some reason when I test the code using integers the code works, but when I use char values it does not. I need help fixing this.
/**
* A class that implements the ADT list by using a chain of linked nodes that
* has a head reference.
*
* @author
*/
public class LListWithTail> implements ListInterface
{
private Node firstNode; // Head reference to first node
private Node lastNode; // Tail reference to last node
private int numberOfEntries; // Number of entries in list
//private int indexOfMinValue = 0;
//private int indexOfMaxValue = 0;
public LListWithTail()
{
initializeDataFields();
}
/**
* Removes all entries from this list.
*/
public void clear()
{
initializeDataFields();
}
// Initializes the class's data fields to indicate an empty list.
private void initializeDataFields()
{
firstNode = null;
lastNode = null;
numberOfEntries = 0;
}
/**
* Retrieves all entries that are in this list in the
* order in which they occur in the list.
* @return A newly allocated array of all the entries
* in the list. Note: If the list is empty, the returned array is empty.
*/
public T[] toArray()
{
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] result = (T[]) new Comparable[numberOfEntries];
int index = 0;
Node currentNode = firstNode;
while ((index < numberOfEntries) && (currentNode != null))
{
result[index] = currentNode.getData();
currentNode = currentNode.getNextNode();
index++;
}
return result;
}
/**
* Adds a new entry to the correct position in this list, so that
* entries in this list are in nondecreasing order.
* The list's size is increased by 1.
* @param newEntry: The object to be added as a new entry.
*/
public void add(T newEntry) //TODO
{
Node newNode = new Node(newEntry);
if(isEmpty())
{
firstNode = newNode;
lastNode = newNode;
numberOfEntries++;
return;
}
else
{
Node current = firstNode;
Node prev = null;
while(current != null){
if(current.getData().compareTo(newEntry) > 0){
if(prev == null){
Node newNode2 = new Node(newEntry,firstNode);
numberOfEntries++;
return;
}
prev.setNextNode(new Node(newEntry,current));
numberOfEntries++;
return;
}
prev = current;
current = current.getNextNode();
}
Node newNode2 = new Node(newEntry);
prev.setNextNode(newNode2);
lastNode = newNode2;
numberOfEntries++;
}
}
private class Node
{
private T data; // Entry in list
private Node next; // Link to next node
private Node(T dataPortion)
{
data = dataPortion;
next = null;
}
private Node(T dataPortion, Node nextNode)
{
data = dataPortion;
next = nextNode;
}
private T getData()
{
return data;
}
private void setData(T newData)
{
data = newData;
}
private Node getNextNode()
{
return next;
}
private void setNextNode(Node nextNode)
{
next = nextNode;
}
} // end Node
}// end LListWithTail
Explanation / Answer
Solution :
Problem is with compareTo function.
compareTo function is defined as given below :
int compareTo(Object o)
current.getData() will return char. which is a primitive data type and newEntry is also primitive data type.
Now when we try to use char data type with compareTo(), It expects an object instead of a literal.
So you need to use objects instead of literals.
to make it work with char , you can directly use "<" or ">".
If you want to use compareTo function then you can do following :
- use wrapper class for primitive types and get the object of wrapper class.
for example get object of Character if you are comparing char.
- Then perform comparison between objects using compareTo function.
If you have any doubts then you can ask here, Thank you.