简体   繁体   English

Java 链表 class

[英]Java LinkedList class

In class, I've implemented my own LinkedList class with a private Node class so I've never run into this issue before.在 class 中,我使用私有Node class 实现了我自己的LinkedList class,所以我以前从未遇到过这个问题。 But now I'm trying to re-do a problem using Java's built-in LinkedList library and am running into trouble.但现在我试图使用 Java 的内置 LinkedList 库重新解决问题,但遇到了麻烦。 (its also been a few years since I last used Java). (距离我上次使用 Java 也有好几年了)。

Lets say I had this simple skeleton.可以说我有这个简单的骨架。 How would I pass the head Node into the function?我如何将头节点传递给 function?

public static void main(String[] args)
{
    LinkedList<Integer> test = new LinkedList<Integer>();
    doSomething(test.get(0));
}


 
private static void doSomething(Node a)
{
    //stuff
}

Also could someone remind me what the difference is between these two?也有人可以提醒我这两者之间有什么区别吗? I know the first you're basically casting the list as a LinkedList but why do so?我知道第一个您基本上将列表转换为 LinkedList 但为什么要这样做?

List<E> test = new LinkedList<E>();
LinkedList<E> test = new LinkedList<E>();

Looking at the documentation for LinkedList , there are no methods that expose the nodes of the list.查看LinkedList的文档,没有公开列表节点的方法。 In fact, LinkedList might even be implemented in a completely different way and not use nodes at all and still have all the properties and performance guarantees of a linked list.事实上, LinkedList甚至可能以完全不同的方式实现,根本不使用节点,并且仍然具有链表的所有属性和性能保证。 It's an implementation detail.这是一个实现细节。

Java's native linked class has some issues. Java 的本机链接 class 存在一些问题。 Iterators can be used to access nodes, but are limited as noted below.迭代器可用于访问节点,但如下所述受到限制。 There is no way to move nodes within a list or from list to list, such as C++ std::list::splice.无法在列表内或从列表到列表移动节点,例如 C++ std::list::splice。

https://en.cppreference.com/w/cpp/container/list/splice https://en.cppreference.com/w/cpp/container/list/splice

For Java, "moving" nodes requires removing and inserting nodes, which involves deallocation for any node removed, and allocation for any node inserted.对于 Java,“移动”节点需要移除和插入节点,这涉及对移除的任何节点进行重新分配,以及对插入的任何节点进行分配。

Java's iterators can't be shallow copied. Java 的迭代器不能被浅拷贝。 An assignment just sets another variable to point to the same iterator object.赋值只是将另一个变量设置为指向同一个迭代器 object。 (C++ iterators don't have this issue). (C++ 迭代器没有这个问题)。

Any removal or insertion of nodes from a list will invalidate all iterators to that list (except for the iterator used to do the remove or insert).任何从列表中删除或插入节点都会使该列表的所有迭代器无效(用于执行删除或插入的迭代器除外)。 (C++ iterators function as expected). (C++ 迭代器 function 符合预期)。

The standard library LinkedList class uses encapsulation to avoid exposing implementation details (like how list nodes are implemented) to the user of the class (you).标准库LinkedList class 使用封装来避免向 class 的用户(您)公开实现细节(例如如何实现列表节点)。

There is no way you can get a reference to the internal list node, save for using advanced techniques likereflection that break encapsulation.除了使用破坏封装的反射等高级技术外,您无法获得对内部列表节点的引用。

Instead of playing around with list nodes and pointers between them, you use the methods the LinkedList class provides to add and retrieve the list elements.您无需使用列表节点和它们之间的指针,而是使用 LinkedList class 提供的方法来添加和检索列表元素。 For example:例如:

LinkedList<Integer> test = new LinkedList<Integer>();
test.add(314);
test.add(879);

Integer first = test.getFirst(); // returns 314
Integer first = test.get(1); // returns 879

The benefit from encapsulation is that JVM implementors are free to change the internal implementation of LinkedList completely without fear of breaking your program.封装的好处是 JVM 实现者可以完全自由地更改 LinkedList 的内部实现,而不必担心破坏您的程序。

You get the same benefit in your own program if you use the List interface instead LinkedList class by writing:如果您使用List接口而不是LinkedList class,您可以在自己的程序中获得相同的好处,方法是:

List<E> test = new LinkedList<E>();

If you do this, you are free to change test from LinkedList to ArrayList or any other list implementation at a later point with no other changes to the code, for example if the application requirements change or if you find that ArrayList gives you better performance.如果您这样做,您可以在稍后将testLinkedList更改为ArrayList或任何其他列表实现,而无需对代码进行其他更改,例如,如果应用程序要求发生变化,或者您发现 ArrayList 为您提供了更好的性能。

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

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