简体   繁体   中英

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. But now I'm trying to re-do a problem using Java's built-in LinkedList library and am running into trouble. (its also been a few years since I last used Java).

Lets say I had this simple skeleton. How would I pass the head Node into the 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?

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. 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. It's an implementation detail.

Java's native linked class has some issues. 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.

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's iterators can't be shallow copied. An assignment just sets another variable to point to the same iterator object. (C++ iterators don't have this issue).

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).

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).

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. 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.

You get the same benefit in your own program if you use the List interface instead LinkedList class by writing:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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