简体   繁体   English

为什么LinkedList需要Java中的节点class?

[英]Why do I need the Node class in Java for LinkedList?

After going over different tutorials on Linked Lists I am seeing some mentioning the JavaNode class for linking to the previous and next nodes and some not using it at all when creating a linkedList.在浏览了关于链接列表的不同教程后,我看到一些提到 Java节点class 用于链接到上一个和下一个节点,而有些在创建链接列表时根本不使用它。

Is the Node class needed for Linked Lists?链表需要节点 class 吗? Why do some tutorials seem to create Linked Lists without it?为什么有些教程似乎在没有它的情况下创建链表? Also read that using the Node class is the "formal" way of creating a linkedlist另请阅读使用节点 class 是创建链表的“正式”方式

If you are asking whether (and why) you need to create Node instances to use a java.util.LinkedList , the answer is: No you don't.如果您询问是否(以及为什么)需要创建Node实例以使用java.util.LinkedList ,答案是:不,您不需要。 The list itself takes care of that.列表本身会处理这个问题。

(Note that the Node class that youlinked to is not a linked list node. It actually denotes a node in an DOM. The actual Node class used internally by java.util.LinkedList is a private class.) (请注意,您链接到Node class 不是链表节点。它实际上表示 DOM 中的节点。java.util.LinkedList内部使用的实际Node java.util.LinkedList是私有的 class。)


If you were asking why linked lists in general require a Node type, the answer is that they don't.如果您问为什么链表通常需要Node类型,答案是它们不需要。

The other way of creating a linked list (that doesn't involve a Node type) is to directly chain the elements of a list to each other.创建链表的另一种方法(不涉及Node类型)是直接将列表的元素彼此链接起来。 This has a couple of consequences:这有几个后果:

  1. This requires the element class itself to have a next field (and possibly a prev field) for chaining the elements.这需要元素 class 本身有一个next字段(可能还有一个prev字段)用于链接元素。

  2. It means that a given element instance can only be a member of one list at a time, and can't be a member of the same list twice.这意味着给定的元素实例一次只能是一个列表的成员,并且不能两次成为同一个列表的成员。

Together, these mean that the nodeless approach is incompatible with the standard java.util.List API.总之,这些意味着无节点方法与标准java.util.List API 不兼容。

The nodeless approach is also bad from the OO design perspective:从 OO 设计的角度来看,无节点方法也很糟糕:

  • By the adding next and prev fields to the element type, you are breaking down abstraction boundaries and the separation of concerns.通过向元素类型添加nextprev字段,您正在打破抽象边界和关注点分离。
  • The element instance now knows about the list that the element is part of.元素实例现在知道该元素所属的列表。
  • The list abstraction only works for certain types of element, and has to take account of which list an element is a member of.列表抽象仅适用于某些类型的元素,并且必须考虑元素是哪个列表的成员。

These things are liable to make the nodeless list abstraction harder to use... and less reusable.这些东西很容易使无节点列表抽象更难使用……并且更难重用。 (Though in limited circumstances, it may still be a good solution.) (虽然在有限的情况下,它可能仍然是一个很好的解决方案。)

You don't technically necessarily need a node class, but the design with a node class is the good design.从技术上讲,您不一定需要节点 class,但具有节点 class 的设计是好的设计。 The design without one is the poor design.没有一个的设计是糟糕的设计。

This answer is slightly opinionated, but based on what we should all have learned in the first or at least the second year of programming, so consensus-based.这个答案有点自以为是,但基于我们在编程的第一年或至少第二年都应该学到的东西,所以是基于共识的。

Say that we have a list of students.假设我们有一份学生名单。 It's now the natural responsibility of each Student object to have (“know”) the student's contact information, courses enrolled in, grades taken, etc. It is not the natural responsibility of a Student object to know that it is part of a linked list, not to mention whether that list is singly or doubly linked.现在每个Student object 的自然责任是拥有(“知道”)学生的联系信息、注册的课程、取得的成绩等。知道它是链表的一部分不是Student object 的自然责任,更不用说该列表是单链接还是双链接了。 For this responsibility we have the Node class.对于这个责任,我们有Node class。

The design with the Node class has the further potential advantage that you can design and code a generic linked list and use it to instantiate a list of students, a list of teachers, a list of courses, etc. Stephen C in the other answer mentions further advantages. Node class 的设计具有进一步的潜在优势,您可以设计和编写通用链表并使用它来实例化学生列表、教师列表、课程列表等。Stephen C 在另一个答案中提到进一步的优势。

Historical background: Where I learned data structures around 1980, we would fit each student record with a next pointer.历史背景:我在 1980 年左右学习数据结构时,我们会为每个学生记录配备一个next指针。 (We learned singly linked lists. Doubly linked lists were only mentioned in passing.) What is nowadays considered the poor design. (我们学的是单向链表,双向链表只是顺便提一下。)现在被认为是糟糕的设计。 I had hoped that it had long gone out of use.我曾希望它早已不再使用。

Performance (skip this paragraph until you really need it:-): The poor design with next and previous references within the business objects like Student will typically perform slightly better.性能(跳过这一段,直到你真的需要它:-):在像Student这样的业务对象中使用nextprevious引用的糟糕设计通常会稍微好一点。 So if you are in a situation where performance is a Very Real Issue, you may consider it.因此,如果您处于性能是一个非常现实的问题的情况下,您可以考虑它。 It is no low-hanging fruit since it pollutes your design, so it will probably come near the bottom of your list of measures to take for better performance.它不是唾手可得的果实,因为它会污染您的设计,因此它可能会在您为提高性能而采取的措施列表中排在最后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM