Java 自定义链接列表与官方链接列表
出于学习目的,我正在伪重新实现官方Java数据结构,我不太清楚为什么官方LinkedList看起来像数组,而我的LinkedList在调试时看起来像链接节点。Java 自定义链接列表与官方链接列表,java,debugging,data-structures,singly-linked-list,Java,Debugging,Data Structures,Singly Linked List,出于学习目的,我正在伪重新实现官方Java数据结构,我不太清楚为什么官方LinkedList看起来像数组,而我的LinkedList在调试时看起来像链接节点。 package Ch02_LinkedList; public class CustomNode { private int data; CustomNode next = null; CustomNode(int data) { this.data = data; } } 这可能只
package Ch02_LinkedList;
public class CustomNode {
private int data;
CustomNode next = null;
CustomNode(int data) {
this.data = data;
}
}
这可能只是调试格式,还是我完全不理解LinkedList的实际实现方式
CustomNode:
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.");
}
}
package Ch02\u链接列表;
导入java.util.LinkedList;
/**
*单链表的自定义实现。
*
*双链接列表还将包含一个“prev”节点。
*/
公共类CustomLinkedList{
私有节点头;
公共void add(int值){
if(this.head==null){
this.head=新的CustomNode(值);
返回;
}
CustomNode current=this.head;
while(current.next!=null){
当前=当前。下一步;
}
current.next=新的CustomNode(值);
}
公共无效预结束(int值){
CustomNode newHead=新CustomNode(值);
newHead.next=this.head;
this.head=newHead;
}
公共void remove(int索引)引发IllegalArgumentException{
if(this.head==null){
返回;
}
如果(索引==0){
this.head=head.next;
返回;
}
自定义节点电流=头;
int currentIndex=0;
while(current.next!=null){
如果(索引==currentIndex+1){
current.next=current.next.next;
返回;
}
当前=当前。下一步;
currentIndex++;
}
抛出新的IllegalArgumentException(“没有找到这样的索引”);
}
公共静态void main(字符串[]args){
CustomLinkedList myList=新建CustomLinkedList();
添加(10);
添加(20);
增加(30);
myList.add(40);
增加(50);
增加(60);
myList.remove(4);
LinkedList officialList=新建LinkedList();
增补(10);
增补(20);
增补(30);
增补(40);
增加(50);
增补(60);
官员。移除(4);
System.out.println(“完成”);
}
}
输出:
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在对话框中有一个选项: 为集合类启用备选视图
选择此选项以更方便的格式显示集合和地图 “数组”视图更方便查看
链接列表的内容,你不这样认为吗
如果您不喜欢方便的格式,请将其关闭
如果您的CustomLinkedList
实现了Collection
,您甚至可能在调试器中获得相同的方便格式,尽管这只是我的猜测,因为我不使用IntelliJ。IntelliJ在对话框中有一个选项:
为集合类启用备选视图
选择此选项以更方便的格式显示集合和地图
“数组”视图更方便查看链接列表的内容,你不这样认为吗
如果您不喜欢方便的格式,请将其关闭
如果您的CustomLinkedList
实现了Collection
,您甚至可能在调试器中得到同样方便的格式,尽管这只是我的猜测,因为我不使用IntelliJ。查看(开放JDK)LinkedList…它实际上不是基于数组的。。。但可能是您的IDE/调试器造成的!??;)什么是智能。。。您是否尝试过实现java.util.List
…?查看(开放JDK)LinkedList…它确实不是基于数组的。。。但可能是您的IDE/调试器造成的!??;)什么是智能。。。您是否尝试过实现java.util.List
…?完美的Andreas。我关闭了该选项,现在我看到了结构的原样。很高兴知道我的Java并不是很烂,哈哈。7年后离开PHP。完美的Andreas。我关闭了该选项,现在我看到了结构的原样。很高兴知道我的Java并不是很烂,哈哈。7年后离开PHP。