我正在尝试使用equlas方法来比较两个Node列表,如果它们相同则返回相等,否则返回false。我的输出只返回false。这是我的代码。
public class LList<T extends Comparable> implements ListInterface<T> {
private Node firstNode; // reference to first node
private int length; // number of entries in list
public LList() {
clear();
} // end default constructor
public LList(T[] items, int numberOfItems) {
clear();
for (int index = numberOfItems - 1; index >= 0; index--)
add(1, items[index]); // more efficient to add to beginning of list
} // end constructor
public final void clear() // NOTICE clear cannot be final in interface
{
firstNode = null;
length = 0;
} // end clear
public boolean add(T newEntry) // OutOfMemoryError possible
{
Node newNode = new Node(newEntry);
if (isEmpty())
firstNode = newNode;
else // add to end of nonempty list
{
Node lastNode = getNodeAt(length);
lastNode.next = newNode; // make last node reference new node
} // end if
length++;
return true;
} // end add
public boolean add(int newPosition, T newEntry) // OutOfMemoryError possible
{
boolean isSuccessful = true;
if ((newPosition >= 1) && (newPosition <= length + 1)) {
Node newNode = new Node(newEntry);
if (isEmpty() || (newPosition == 1)) // case 1: add to beginning of
// list
{
newNode.next = firstNode;
firstNode = newNode;
} else // case 2: list is not empty and newPosition > 1
{
Node nodeBefore = getNodeAt(newPosition - 1);
Node nodeAfter = nodeBefore.next;
newNode.next = nodeAfter;
nodeBefore.next = newNode;
} // end if
length++;
} else
isSuccessful = false;
return isSuccessful;
} // end add
public T remove(int givenPosition) {
T result = null; // return value
if ((givenPosition >= 1) && (givenPosition <= length)) {
assert !isEmpty();
if (givenPosition == 1) // case 1: remove first entry
{
result = firstNode.data; // save entry to be removed
firstNode = firstNode.next;
} else // case 2: givenPosition > 1
{
Node nodeBefore = getNodeAt(givenPosition - 1);
Node nodeToRemove = nodeBefore.next;
Node nodeAfter = nodeToRemove.next;
nodeBefore.next = nodeAfter; // disconnect the node to be
// removed
result = nodeToRemove.data; // save entry to be removed
} // end if
length--;
} // end if
return result; // return removed entry, or
// null if operation fails
} // end remove
public boolean replace(int givenPosition, T newEntry) {
boolean isSuccessful = true;
if ((givenPosition >= 1) && (givenPosition <= length)) {
assert !isEmpty();
Node desiredNode = getNodeAt(givenPosition);
desiredNode.data = newEntry;
} else
isSuccessful = false;
return isSuccessful;
} // end replace
public T getEntry(int givenPosition) {
T result = null; // result to return
if ((givenPosition >= 1) && (givenPosition <= length)) {
assert !isEmpty();
result = getNodeAt(givenPosition).data;
} // end if
return result;
} // end getEntry
public boolean contains(T anEntry) {
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null)) {
if (anEntry.equals(currentNode.data))
found = true;
else
currentNode = currentNode.next;
} // end while
return found;
} // end contains
public int getLength() {
return length;
} // end getLength
public boolean isEmpty() {
boolean result;
if (length == 0) // or getLength() == 0
{
assert firstNode == null;
result = true;
} else {
assert firstNode != null;
result = false;
} // end if
return result;
} // end isEmpty
public boolean isFull() {
return false;
} // end isFull
public void display() {
// iterative
Node currentNode = firstNode;
while (currentNode != null) {
System.out.println(currentNode.data);
currentNode = currentNode.next;
} // end while
// recursive
// displayChain(firstNode);
// System.out.println();
} // end display
private void displayChain(Node nodeOne) {
if (nodeOne != null) {
System.out.print(nodeOne.data + " ");
displayChain(nodeOne.next);
} // end if
} // end displayChain
/**
* Task: Returns a reference to the node at a given position. Precondition:
* List is not empty; 1 <= givenPosition <= length.
*/
private Node getNodeAt(int givenPosition) {
assert !isEmpty() && (1 <= givenPosition) && (givenPosition <= length);
Node currentNode = firstNode;
// traverse the list to locate the desired node
for (int counter = 1; counter < givenPosition; counter++)
currentNode = currentNode.next;
assert currentNode != null;
return currentNode;
} // end getNodeAt
private class Node {
private T data; // entry in list
private Node next; // link to next node
private Node(T dataPortion) {
data = dataPortion;
next = null;
} // end constructor
private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
} // end constructor
} // end Node
@Override
public boolean remove(T anObject) {
// TODO Auto-generated method stub
return false;
}
@Override
public int getPosition(T anObject)
{
int position = 0;
Node currentNode = firstNode;
for(int i = 0; i < length; i++)
{
if(anObject.equals(currentNode.data))
{
position = i + 1;
}
currentNode = currentNode.next;
}
return position;
}
@Override
public void moveToEnd() {
// TODO Auto-generated method stub
}
public void addAll(T[] items)
{
for (int index = 0; index < items.length; index++)
{
Node newNode = new Node(items[index]);
if (isEmpty())
firstNode = newNode;
else
{
Node lastNode = getNodeAt(length);
lastNode.next = newNode; // make last node reference new node
add(items[index]);
}
}
}
public boolean equals(LList<String> newList)
{
boolean results = false;
Node currentNode = firstNode;
for(int i = 0; i < length; i++)
{
if (newList.equals(currentNode.data))
{
results = true;
}
currentNode = currentNode.next;
}
return results;
}
@SuppressWarnings("unchecked")
public LList<String> getAllLessThan(Comparable<T> anObject) {
LList<String> newList = new LList();
Node currentNode = firstNode;
for (int i = 0; i < length; i++)
{
if (anObject.compareTo(currentNode.data) > 0)
{
newList.add((String) currentNode.data);
}
currentNode = currentNode.next;
}
return newList;
}
public String getMin() {
// TODO Auto-generated method stub
return null;
}
public String removeMin() {
// TODO Auto-generated method stub
return null;
这是我的示例程序,我在其中测试我的equals方法是否正确。
public class Homework3Driver {
public static void main(String[] args) {
String[] names = {"Abby", "Bobby", "Carla", "Doug"};
LList<String> nameList = new LList(names, 4);
String[] newNames = {"Edgar", "Frank"};
nameList.addAll(newNames);
System.out.println("Output should be Abby, Bobby, Carla, Doug, Edgar, Frank");
nameList.display();
System.out.println();
String[] compareArr1 = {"Abby", "Bobby", "Carla", "Doug", "Edgar", "Frank"};
LList<String> compareList1 = new LList<String>(compareArr1, 6);
String[] compareArr2 = {"Bobby", "Carla", "Doug", "Edgar", "Frank"};
LList<String> compareList2 = new LList<String>(compareArr2, 5);
String[] compareArr3 = {"Abby", "Bobby", "Carla", "Doug", "Edgar"};
LList<String> compareList3 = new LList<String>(compareArr3, 5);
String[] compareArr4 = {"Abby", "Bobby", "Carla", "Doug", "Edgar", "Frank", "Georgie"};
LList<String> compareList4 = new LList<String>(compareArr4, 7);
String[] compareArr5 = {"Abby", "Bobby", "Carla", "Chris", "Doug", "Edgar", "Frank"};
LList<String> compareList5 = new LList<String>(compareArr5, 7);
System.out.println("Output should be true: " + nameList.equals(compareList1));
System.out.println("Output should be false: " + nameList.equals(compareList2));
System.out.println("Output should be false: " + nameList.equals(compareList3));
System.out.println("Output should be false: " + nameList.equals(compareList4));
System.out.println("Output should be false: " + nameList.equals(compareList5) + "\n");