Java LinkedList中的Add方法不起作用
我将此作为Java类的额外练习,但我似乎不明白为什么我的add方法不起作用。我试着用调试器一行一行地浏览代码,但我看不出哪里出了问题。这显然是一个逻辑错误。当我测试add方法时,它似乎可以工作,但我猜它没有正确链接节点的链接,或者没有存储数据 因此,任务是编写一个链表(不添加重复项)。我将Node类作为LinkedSet类的内部函数来执行。我们的教科书建议,对于这个特殊的作业。 这是我正在使用的add方法Java LinkedList中的Add方法不起作用,java,linked-list,nodes,singly-linked-list,Java,Linked List,Nodes,Singly Linked List,我将此作为Java类的额外练习,但我似乎不明白为什么我的add方法不起作用。我试着用调试器一行一行地浏览代码,但我看不出哪里出了问题。这显然是一个逻辑错误。当我测试add方法时,它似乎可以工作,但我猜它没有正确链接节点的链接,或者没有存储数据 因此,任务是编写一个链表(不添加重复项)。我将Node类作为LinkedSet类的内部函数来执行。我们的教科书建议,对于这个特殊的作业。 这是我正在使用的add方法 public class LinkedSet <T> implements S
public class LinkedSet <T> implements SetInterface <T> {
private Node firstNode;
private int numberOfEntries;
public LinkedSet (){
firstNode = null;
numberOfEntries = 0;
}
public boolean add(T newEntry){
boolean result = false;
Node aNewNode = new Node (newEntry);
//If Node is currently null then add data to front
if(firstNode == null)
firstNode = aNewNode;
else
{
//Add newEntry if it's not already stored.
if(!contains(newEntry) && numberOfEntries != 0)
{
aNewNode.next = firstNode;
firstNode.next = aNewNode;
firstNode = aNewNode;
result = true;
}
else
result = false;
}
return result;
}//End add
public boolean contains(T anEntry){
boolean found = false;
while (!found && (firstNode != null))
{
if(anEntry.equals(firstNode.getData()))
found = true;
else
firstNode = firstNode.getNextNode();
}
return found;
}
private class Node {
private T data;
private Node next;
private Node (T theData){
data = theData;
next = null;
}
private Node(T theData, Node nextNode){
data = theData;
next = nextNode;
}
} //End Node class
}End LinkedSet
公共类LinkedSet实现SetInterface{
私有节点firstNode;
私人int numberOfEntries;
公共链接集(){
firstNode=null;
numberOfEntries=0;
}
公共布尔加法(T newEntry){
布尔结果=假;
节点aNewNode=新节点(newEntry);
//若节点当前为空,则将数据添加到前端
if(firstNode==null)
firstNode=aNewNode;
其他的
{
//如果尚未存储,则添加新条目。
如果(!contains(newEntry)&&numberOfEntries!=0)
{
aNewNode.next=firstNode;
firstNode.next=aNewNode;
firstNode=aNewNode;
结果=真;
}
其他的
结果=假;
}
返回结果;
}//结束添加
公共布尔包含(T anEntry){
布尔值=false;
而(!found&(firstNode!=null))
{
if(anEntry.equals(firstNode.getData()))
发现=真;
其他的
firstNode=firstNode.getNextNode();
}
发现退货;
}
私有类节点{
私有T数据;
私有节点下一步;
专用节点(T数据){
数据=数据;
next=null;
}
专用节点(T theData,Node nextNode){
数据=数据;
next=nextNode;
}
}//结束节点类
}端连接集
此外,这是add的测试方法(需要编写单独的测试应用程序,并在单独的主类中完成)
private static boolean testAdd(SetInterface aSet,String[]contentsToAdd){
布尔结果=假;
for(int index=0;index
还有一些其他的方法,但直到我可以得到添加方法的工作,我不能与他们做太多,所以我很确定问题就在这里的某个地方。
我已经在网上搜索过类似的问题,但我仍然看不出它在哪里。任何帮助都是感激的,我已经被它弄得太久了
if(firstNode == null)
firstNode = aNewNode;
在这种情况下,您应该返回true
if(!contains(newEntry) && numberOfEntries != 0)
这个测试没有多大意义。反过来说,它更有意义:
if(numberOfEntries != 0 && !contains(newEntry))
因为如果没有条目,调用contains()
没有意义,但是contains()
已经知道,由于firstNode
如果numberOfEntries
为零,则为null,因此它应该是
if (!contains(newEntry))
注意:您没有维护
numberOfEntries
我没有彻底阅读您的所有代码,但这里有一个错误:
if(firstNode == null)
firstNode = aNewNode;
它应该增加numberOfEntries
因此,numberOfEntries
始终为零,并且add无法正常工作
类似地,您也不会在
else
中维护numberOfEntries
。包含的方法也有问题,您正在更改firstNode引用,您不应该更改它
public boolean contains(T anEntry){
boolean found = false;
Node ptr = firstNode;
while (!found && (ptr != null))
{
if(anEntry.equals(ptr.getData()))
found = true;
else
ptr = ptr.getNextNode();
}
return found;
}
firstNode=firstNode.getNextNode();
public boolean contains(T anEntry){
boolean found = false;
Node ptr = firstNode;
while (!found && (ptr != null))
{
if(anEntry.equals(ptr.getData()))
found = true;
else
ptr = ptr.getNextNode();
}
return found;
}