链接列表库存程序的findItem方法

时间:2015-05-10 23:15:11

标签: java linked-list inventory

对于我的上一次作业,我使用数组制作了一个库存程序。我们的最后一项任务是将数组更改为链接列表。我们不是要使用LinkedList类,我们是创建自己的。我让我的程序正常工作,但我的导师说它只是好的,但可能会更好。主要是我的findLtem方法,在我的InventoryLL类中。我的问题是:有更好的方法可以在我的链接列表中找到一个项目吗?任何建议都会非常感激,到目前为止我已经完成了每项任务的100%,只是努力完成任务,并尽可能多地学习:)

public ItemNode findItem() 
{
    boolean found = false;
    int inputID = 0;
    ItemNode current = head;
        try{
            System.out.print("\nGreetings, please enter the ID number for item:\n");
            inputID = scannerObject.nextInt();
            scannerObject.nextLine();
        while (found != true){
         if (current.getID() == inputID){
             found = true;
             break;
            }
         current = current.getNext();
        }      
    }catch(Exception e)
            {
            System.out.println("\nERROR!");
            }
    return current;
}      

1 个答案:

答案 0 :(得分:0)

简短的回答是,如果不将其变成(例如)跳过列表(有序链表,包括某种“快速路径”),就无法提高效率。

Wikipedia has a well written article that explains both the implementation of the list, as well as the searching of it

虽然可能有点超出范围。