Java 链表 find() 方法。如何

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19763102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:00:22  来源:igfitidea点击:

Linked list find() method . How to

java

提问by Ernusc

okey, I have a linked list (not collections) and I created a new method for find an object in my Linked list. So something like this:

好吧,我有一个链表(不是集合),我创建了一个新方法来在我的链表中查找对象。所以像这样:

 public Object find(Linked obj) {

        Linked newObj = firstLink;

        while(newObj != null) {

            if(newObj == obj) {
                return obj;
            }
            else {

                newObj = newObj.next;
            }
        }

By the way, I have 2 classes: Linkedand Linkedlist. In the first one, I have a reference to the next node and display function.The main action is in Linkedlist where I have all methods like insert, display and a firstLinkreference (the last inserted node in my list) ( my find() method also is in this class). So in my main function I am doing this:

顺便说一下,我有2 个类:LinkedLinkedlist. 在第一个中,我引用了下一个节点和显示函数。主要操作在 Linkedlist 中,我有所有方法,如插入、显示和firstLink引用(列表中最后插入的节点)(我的 find() 方法也在这个类中)。所以在我的主要功能中,我这样做:

Linkedlist obj = new Linkedlist();
obj.insert("Auto");

Linkedlist obj2 = new Linkedlist();
obj2.insert("Moto");

And how can I call my method find()to check if my Linkedlisthas (for example) obj2?

以及如何调用我的方法find()来检查我Linkedlist是否拥有(例如)obj2

采纳答案by CodingBird

Think it this way:

这样想:

You have Linked class (this is a linked list node, and each node should have the next pointer and the element inside the node). So this class should have constructor,setter,and getter methods.

你有 Linked 类(这是一个链表节点,每个节点都应该有下一个指针和节点内的元素)。所以这个类应该有构造函数、setter 和 getter 方法。

On the other hand, Linkedlist class is your main class that manage the Linked object (that is managing the linked list node). In this class you should have reference to the root node (the very first node you insert). So in your program, you should only have one/more Linked object and Linkedlist as your main class.

另一方面,Linkedlist 类是您管理 Linked 对象(即管理链表节点)的主类。在这个类中,您应该引用根节点(您插入的第一个节点)。所以在你的程序中,你应该只有一个/多个 Linked object 和 Linkedlist 作为你的主类。

Linked root = new Linked("Auto",null); //here Linked constructor takes 2 parameters, the element and the next pointer.
//Since you only inserted one element so far, the next element should be null.

//Insert another element
insertAtEnd("Moto");

public void insertAtEnd(String element){
    Linked curr = root;
    while(curr.next != null) curr = curr.next;
    curr.setNext(new Linked(element,null);
}

public Linked findElement(String element){
    Linked curr = root;
    while(curr!=null){
         if(curr.getElement().equals(element)) return curr;
         else curr = curr.next;
    }
    return null; //element not found
}

回答by Stefan Freitag

  • To compare a single object in LinkedList to another one:

    Implement the method equals()for Linked. Within this method check if the Strings contained in both objects (current object in the iteration and the object to find) are the same. To do so, you should use the String.equals(String other)method

  • To compare two LinkedLists, write another equals()method. This time for LinkedList. In this method you should compare e.g. the number of elements in boths lists. If the lists contain the same number of elements, you can use the previously defined Linked.equals()to compare the lists item by item.

  • 将 LinkedList 中的单个对象与另一个对象进行比较:

    实现该方法equals()Linked。在此方法中,检查两个对象(迭代中的当前对象和要查找的对象)中包含的字符串是否相同。为此,您应该使用该String.equals(String other)方法

  • 要比较两个 LinkedList,请编写另一种equals()方法。这次为LinkedList. 在这种方法中,您应该比较例如两个列表中的元素数量。如果列表包含相同数量的元素,您可以使用之前定义的Linked.equals()来逐项比较列表。