自定义链表与官方链表

Custom LinkedList vs official LinkedList

我正在伪造官方 Java 数据结构用于学习目的并且 我不太清楚为什么官方 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.");
    }
}

输出:

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

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

"array" 视图更方便查看 LinkedList 的内容,你不觉得吗?

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

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