java - 自定义链表与官方链表

标签 java debugging data-structures singly-linked-list

出于学习目的,我伪重新实现了官方 Java 数据结构,我不太确定为什么官方 LinkedList 看起来像一个数组,而我的 LinkedList 在调试时看起来像链式节点。

这可能只是调试格式还是我完全误解了 LinkedList 的实际实现方式?

自定义节点:

package Ch02_LinkedList;

public class CustomNode {
    private int data;

    CustomNode next = null;

    CustomNode(int data) {
        this.data = data;
    }
}

自定义链接列表:

package Ch02_LinkedList;

import java.util.LinkedList;

/**
 * Custom implementation of a singly linked list.
 *
 * A double linked list would also contain a "prev" node.
 */
public class CustomLinkedList {
    private CustomNode head;

    public void add(int value) {
        if (this.head == null) {
            this.head = new CustomNode(value);

            return;
        }

        CustomNode current = this.head;

        while (current.next != null) {
            current = current.next;
        }

        current.next = new CustomNode(value);
    }

    public void prepend(int value) {
        CustomNode newHead = new CustomNode(value);
        newHead.next = this.head;
        this.head = newHead;
    }

    public void remove(int index) throws IllegalArgumentException {
        if (this.head == null) {
            return;
        }

        if (index == 0) {
            this.head = head.next;

            return;
        }

        CustomNode current = head;
        int currentIndex = 0;

        while (current.next != null) {
            if (index == currentIndex+1) {
                current.next = current.next.next;

                return;
            }

            current = current.next;
            currentIndex++;
        }

        throw new IllegalArgumentException("No such a index has been found.");
    }

    public static void main(String[] args) {
        CustomLinkedList myList = new CustomLinkedList();
        myList.add(10);
        myList.add(20);
        myList.add(30);
        myList.add(40);
        myList.add(50);
        myList.add(60);
        myList.remove(4);

        LinkedList<Integer> officialList = new LinkedList<>();
        officialList.add(10);
        officialList.add(20);
        officialList.add(30);
        officialList.add(40);
        officialList.add(50);
        officialList.add(60);
        officialList.remove(4);

        System.out.println("Done.");
    }
}

输出:

enter image description here

最佳答案

IntelliJ 在 Preferences 中有一个选项对话框:

Enable alternative view for Collection classes
Select this option to display collections and maps in a more convenient format.

“数组” View 更方便查看 LinkedList 的内容,您不觉得吗?

如果您不喜欢这种方便的格式,请将其关闭。

如果您的 CustomLinkedList 实现了 Collection,您甚至可能会在调试器中获得同样方便的格式,尽管这只是我的猜测,因为我不使用 IntelliJ .

关于java - 自定义链表与官方链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48632830/

相关文章:

java - 在 Java 中使用完整的构造函数调用作为 lambda 表达式的方法引用

java - 动态创建 FormLayout 行和列(在运行时)

debugging - SSE2 : _mm_mul_ps fails on OS X in case of GCC 4. 2 和 O0 优化

java - 从数组中找到最多包含两个连续元素的最大和

java - "debug as"与导出的 APK 有何不同

javascript - getElementById 在脚本中没有返回任何内容或问题?

c - C中的数据结构——在列表的开头插入节点

algorithm - 打印安装包所需的最小包集

c++ - vector push_back move 实现

java - 当我从 Eclipse (Juno) 运行 Sonar 本地分析时出错 - 我使用 maven 来构建