Java LinkedList 类

huangapple go评论46阅读模式
英文:

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. (it's 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>();
英文:

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&lt;Integer&gt; test = new LinkedList&lt;Integer&gt;();
	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&lt;E&gt; test = new LinkedList&lt;E&gt;();
LinkedList&lt;E&gt; test = new LinkedList&lt;E&gt;();

答案1

得分: 1

查看LinkedList的文档,没有公开暴露列表节点的方法。实际上,LinkedList 可能以完全不同的方式实现,甚至可能根本不使用节点,但仍具有链表的所有属性和性能保证。这是一个实现细节。

英文:

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.

答案2

得分: 0

Java的本机链接类存在一些问题。迭代器可用于访问节点,但受到以下限制。没有办法在列表内或从列表移动节点,例如C++ std::list::splice。

对于Java,“移动”节点需要删除和插入节点,这涉及到移除的任何节点的释放和插入的任何节点的分配。

Java的迭代器无法进行浅复制。赋值只是将另一个变量指向相同的迭代器对象。(C++迭代器没有这个问题)。

从列表中删除或插入节点将使该列表上的所有迭代器无效(除了用于执行删除或插入的迭代器)。(C++迭代器的行为符合预期)。

英文:

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

答案3

得分: 0

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 like reflection 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&lt;E&gt; test = new LinkedList&lt;E&gt;();

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 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 like reflection 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&lt;Integer&gt; test = new LinkedList&lt;Integer&gt;();
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&lt;E&gt; test = new LinkedList&lt;E&gt;();

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.

huangapple
  • 本文由 发表于 2020年7月31日 05:45:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63181822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定