如何在Java链表中搜索

如何在Java链表中搜索,java,linked-list,inventory,Java,Linked List,Inventory,我正在为我的intro Java类做最后一项作业。我们正在做一个链表(我们自己建立)库存计划。我的findItem方法有问题。如何按项目编号搜索我的链接列表??我这样做是可行的,但我需要摆脱休息。我的指导老师说我“需要设置一个复合条件,即!发现您有且不为空。”我已经尝试了几种不同的变体,但无法获得它。请,任何帮助,建议,或例子将不胜感激,它的最后一周,一切都是由于明天。我的主要问题是InventoryLL类中的findItem(),但我发布了整个上下文代码 InventoryLL ItemNod

我正在为我的intro Java类做最后一项作业。我们正在做一个链表(我们自己建立)库存计划。我的findItem方法有问题。如何按项目编号搜索我的链接列表??我这样做是可行的,但我需要摆脱休息。我的指导老师说我“需要设置一个复合条件,即!发现您有且不为空。”我已经尝试了几种不同的变体,但无法获得它。请,任何帮助,建议,或例子将不胜感激,它的最后一周,一切都是由于明天。我的主要问题是InventoryLL类中的findItem(),但我发布了整个上下文代码

InventoryLL

ItemNode

inventUserLL

更新


假设root持有第一个节点值

ItemNode root = ...;

public ItemNode findNode(int value){
   if(root !=null){
       ItemNode temp = root;
       while(temp!=null){
           if(temp.getID() == value) return temp;
           temp = temp.next;
       }
   }
   return null;
}

我可以理解您想删除“break;”在你的方法中,让函数仍然工作

你试过用“break;”来代替吗有“返回电流”吗

public ItemNode findItem(){
    int inputID;
    boolean found = false;
    ItemNode current = null;
        try{
        System.out.print("\nGreetings, please enter the ID number for item:\n");
        inputID = scannerObject.nextInt();
        scannerObject.nextLine();
            for (current = head; !found; current = current.getNext()){
                if (current.getID() == inputID){                    
                    return current; // return the result when you find the node
                    }
                }       
            }catch(Exception e)
            {
                System.out.println("\nERROR!");
            }
    return current;
}
如果我没有错的话,“复合条件”如下:

if (head != null) {
    while ((current != null) && (!found)) {
        // check to see if you have the right value, set `found` if you do
        // otherwise, continue
        if (!found) { // do this otherwise you'll return the next value everytime
            current = current.getNext();
        }
    }
    if (!found) { // if you're at the end of the list and you didn't find the value
        // set current = to something you want to return if the value was not found
    }

}
return current;

您可以在不丢失语义的情况下删除中断。现在,头部的定义似乎缺失了

讲师的意思是:如果搜索的对象不在列表中,则循环将导致
NullPointerException
。因此,中断循环的条件还必须包含对现有下一个元素的检查,例如:

for (current = head; !found && current.hasNext(); current = current.getNext()) {

你应该检查一下,不要在
head==null

的情况下进入这个循环。我的指导老师希望我们只使用一个返回语句。请参阅我的更新答案非常感谢!这似乎是可行的,除了我还有一个问题。当我搜索一个不存在的项目时,它返回最后输入的项目。例如,如果我只有7个条目,搜索ID号为8或9,它将返回条目号7。这是因为我没有在这个代码段中包含该逻辑。在while循环之外,如果(!found){current=null;},则使用另一个
(或者如果没有找到它,则使用希望返回的任何值)。将其放在返回当前值之前。我将把代码添加到我的代码段中。好的,我明白了。。。非常感谢你!我想这就成功了。没有遇到任何其他问题。。。我感谢你的帮助:)除了我遇到的另一个问题外,一切正常。当我有7个项目,并搜索项目7时,它返回null。当我试图找到列表中的最后一个元素时,其他一切都很好。我将上面的代码更新为现在的代码。不过,我发现了我的问题。。。我将
while((current.getNext()!=null)和(!found))更改为
while((current!=null)和(!found))
ItemNode root = ...;

public ItemNode findNode(int value){
   if(root !=null){
       ItemNode temp = root;
       while(temp!=null){
           if(temp.getID() == value) return temp;
           temp = temp.next;
       }
   }
   return null;
}
public ItemNode findItem(){
    int inputID;
    boolean found = false;
    ItemNode current = null;
        try{
        System.out.print("\nGreetings, please enter the ID number for item:\n");
        inputID = scannerObject.nextInt();
        scannerObject.nextLine();
            for (current = head; !found; current = current.getNext()){
                if (current.getID() == inputID){                    
                    return current; // return the result when you find the node
                    }
                }       
            }catch(Exception e)
            {
                System.out.println("\nERROR!");
            }
    return current;
}
if (head != null) {
    while ((current != null) && (!found)) {
        // check to see if you have the right value, set `found` if you do
        // otherwise, continue
        if (!found) { // do this otherwise you'll return the next value everytime
            current = current.getNext();
        }
    }
    if (!found) { // if you're at the end of the list and you didn't find the value
        // set current = to something you want to return if the value was not found
    }

}
return current;
for (current = head; !found && current.hasNext(); current = current.getNext()) {