【问题标题】:Difference between two lists in JavaJava中两个列表之间的区别
【发布时间】:2017-04-03 07:27:13
【问题描述】:

我开始用 Java 学习数据结构和算法。

我从列表开始,我学到的第一个概念是数组和列表的区别。

现在在列表中,我学习如何将元素插入到列表中并在屏幕上显示。我得到了两段代码。第一个我非常了解;第二个令人困惑的地方。

我的困惑是这里的 Node 是什么?一开始我们声明Node head;。那么这里的Node基本上是什么?

它是预定义的数据类型还是什么?另外,第二个程序是如何进行的?第二个程序与第一个程序有何不同?又有什么区别呢?

我也知道我们在这里使用 util,但我仍然想知道,因为人们如何在行业中使用它。

第一种方法:

import java.util.*;

public class TestCollection7 {
    public static void main(String args[]) {

        LinkedList<String> al = new LinkedList<String>();
        al.add("Ravi");
        al.add("Vijay");
        al.add("Ravi");
        al.add("Ajay");

        Iterator<String> itr = al.iterator();
        while(itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
}

第二种方法:

package javaapplication3;

public class LIstTEst {
    Node head;

    static class Node {
        int data;
        Node next;
        Node(int d) {
            data = d;
            next = null;
        }
    }

    public void PrintList() {
        Node n = head;
        while(n != null){
            System.out.print(n.data+" ");
            n = n.next;
        }
    }

    public static void main(String args[]) {
        LIstTEst llist = new LIstTEst();
        llist.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(7);

        llist.head.next = second;
        second.next = third; = third;

        llist.PrintList();
    }
}

【问题讨论】:

  • 请注意,您的第二个示例存在缺陷:首先,您将 head.next 设置为 second 即可。但是之后,您将其设置为“第三”,这是错误的。您应该将second.next 设置为third。照原样,您从列表中丢失了second 节点。实际上,列表实现应该有一个add 方法来创建一个新节点,设置它的值并将尾部的next 引用设置为新节点。为了清楚起见:您的列表以“head->third”结尾,但应该是“head->second->third”。
  • @Fildor 你说得对,我在本地调试器中更改了我的代码。
  • @Fildor 所以基本上你建议我应该使用add 方法而不是Node 和所有。
  • 不,您仍然需要 Node 类。但是您的客户不应该直接处理它。您的列表实现应该提供一个从处理节点引用中抽象出来的 API。例如,用户应该使用的所有方法都是void add(int newValue)。看LinkedList的API。它也使用节点,但作为客户端,您不知道这一点。
  • 它甚至可以编译吗? second.next = third; = third; 看起来很可疑(更改为 revision 3)。

标签: java list data-structures


【解决方案1】:

Node 是一个嵌套在LIstTEst 中的类。

你有它的定义:

static class Node {
    int data;
    Node next;
    Node(int d) {
        data = d;
        next = null;
    }
}

它代表链表中的一个链接。它包含一个数值 (data) 和对列表中下一个 Node 的引用。

Node head;

LIstTEst 的成员,它指的是列表中的第一个链接。

请注意,缺少已发布的 LIstTEst 实现。它应该使用add 方法而不是main 创建Nodes 并直接操作链接。

【讨论】:

  • 实际上,在发布的示例中,存在缺陷。它丢失了second 节点。
  • @Eran 很好的解释。一个快速的问题,我应该首先或第二个理解列表。以后我可能会因为列表中的问题而度过艰难的日子。
  • @shankysingh 取决于你想学什么。你想学习Java List 接口吗?然后玩弄 List 实现。您想了解作为概念的数据结构“列表”吗?使用自己的实现。
  • @Fildor 当您说使用自己的解释时,您指的是什么。任何例子。
  • @shankysingh 这取决于你的目的。一般来说,绝对应该使用现有的类(例如 java.util.LinkedList)而不是重新发明轮子。如果您想学习如何自己实现 LinkedList,则应该只关心第二个实现(当然您需要更好的实现)。
【解决方案2】:

那个

public class LIstTEst

只不过是实现列表第一步。而您的第一段源代码使用了作为“标准”Java 集合类的一部分提供的链表实现。

“链接列表”(一般而言)只不过是一个连接事物(相同类型)的列表。那些“东西”是使用内部的Node 类来表达的。

因此,真正的答案是:避免混淆概念和实现。为了了解 Java 是如何处理“链表”的;您应该首先退后一步,了解“链表”本身的概念。例如,通过阅读其Wikipedia 条目:

在计算机科学中,链表是数据元素的线性集合,称为节点,每个节点都通过指针指向下一个节点。它是由一组节点组成的数据结构,这些节点一起表示一个序列。在最简单的形式下,每个节点都由数据和对序列中下一个节点的引用(换句话说,链接)组成。这种结构允许在迭代期间从序列中的任何位置有效地插入或删除元素。更复杂的变体添加额外的链接,允许从任意元素引用中高效插入或删除。

换句话说:编程语言是关于实现该给定语言的通用算法和数据结构。因此,非常建议您通过了解这些常见概念来开始您的研究。

除此之外:您可能会发现第二个示例令人困惑的原因:

  • 可能你还没有了解inner classes
  • 示例不完整;仍然缺少添加或删除列表元素的重要方法!

【讨论】:

  • 感谢您的回答。是的,我会看看内部类。
  • 不客气。但为了记录:这些都是非常基本的东西。学习的一个要素是自己寻找信息来源。要求其他人解释工作;是的;但是自己找到答案是 A) 真正的乐趣和 B) 长期“学习体验”的真正所在。
【解决方案3】:
public class LIstTEst {
    Node head; // ==> Declaring a Class level variable.

    static class Node { // Create a static class
        int data; // Integer data type variable.
        Node next; // Class refrernce.

        Node(int d) { // Create a constructor.
            data = d; // Holding the current value.
            next = null;
        }
    }

    public void PrintList() {
        Node n = head;
        while (n != null) {
            System.out.print(n.data + " ");
            n = n.next;
        }
    }

    public static void main(String args[]) {
        LIstTEst llist = new LIstTEst(); // Creating a object of the mentioned
                                         // class.

        llist.head = new Node(1);        // Creating the Node class object by Class
                                         // level refrence.

        Node second = new Node(2);       // As this is static class, inside one class,
                                         // so we created directly to object of Node
                                         // class by passing the parameter to the
                                         // constructor.
        Node third = new Node(7);

        llist.head.next = second; // Just transfering the current reference to
                                  // next level reference before that
                                  // constructor is making null for each
                                  // executions.
        llist.head.next = third;

        llist.PrintList(); // Finally for printing purpose it is calling.
    }
}

【讨论】:

    【解决方案4】:

    第一个例子

    LinkedList 是 Java 中链表数据结构的实现。

    第二个例子

    它是手动编写的相同数据结构的实现。 Node 是你的类,它描述了这个链表中的一个单元格。

    两种实现方式不同。 Java 的实现提供了一系列方便的方法来处理列表,而您的则没有。您可以从实现add 方法开始。

    // Appends the specified element to the end of this list.
    public void add(int data) {
    
        // Create Node only in case of first element
        if (head == null) {
            head = new Node(data);
        }
    
        Node temp = new Node(data);
        Node current = head;
    
        // Check if there is first element
        if (current != null) {
    
            // Starting at the head node, iterate to the end of the list and then add element after last node
            while (current.getNext() != null) {
                current = current.getNext();
            }
    
            // The last node's "next" reference set to our new node
            current.setNext(temp);
        }
    }
    

    什么是链表?

    长话短说:链表是一串单元格。每个细胞都知道下一个细胞。最后一个单元格知道没有下一个单元格。一个链表类(操作这些单元(节点))包含第一个节点的引用。

    看看这个互动演示:https://visualgo.net/list

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 2016-11-16
      • 2012-06-25
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多