简体   繁体   English

链表 addLast() 方法未按预期工作

[英]Linked List addLast() method is not working as expected

As per the definition addLast() , the addLast() method is used to add element to the last of the list.根据定义addLast() , addLast() 方法用于将元素添加到列表的最后一个。 But here in the following code that's not happening.但是在下面的代码中没有发生。

LinkedList<Integer> demo = new LinkedList<>();
demo.addLast(15);
demo.addFirst(1);
demo.add(10);
System.out.println(demo);

The output is output 是

[1, 15, 10]

But as per the definition the output should have been但根据定义 output 应该是

[1, 10, 15]

Because the addLast() method should be adding 15 to the last of the list.因为 addLast() 方法应该将 15 添加到列表的最后一个。 I'm not sure what exactly is the issue here.我不确定这里到底是什么问题。

Code snippet: Code snippet代码片段:代码片段

addLast adds an item at the end of the list - it doesn't mean it will remain at the end of list when additional items are added. addLast在列表末尾添加一个项目 - 这并不意味着添加其他项目时它将保留在列表末尾。

Let's track the code:让我们跟踪代码:

  • you start with an empty list.你从一个空列表开始。
  • addLast(15) will add 15 at the end of this list, so it's now [15] addLast(15)将在这个列表的末尾添加 15,所以现在是 [15]
  • addFirst(1) will add 1 at the beginning of the list, so it's now [1, 15] addFirst(1)将在列表的开头添加 1,所以现在是 [1, 15]
  • add(10) will add 10 at the end of the list, so it's now [1, 15, 10] add(10)将在列表末尾添加 10,所以现在是 [1, 15, 10]

addLast() does add 15 to the end of the list, it transfers it from [] to [15] . addLast()确实将 15 添加到列表的末尾,它将它从[]转移到[15] Later you use add() , which appends another element to the end of the list (after 15 ).稍后您使用add() ,它将另一个元素附加到列表的末尾(在15之后)。

From the documentation of add() :add()的文档中:

public boolean add(E e) Appends the specified element to the end of this list. public boolean add(E e) 将指定元素附加到此列表的末尾。 This method is equivalent to addLast(E).此方法等效于 addLast(E)。

well as Obvious as this can be the add() adds at the last of the list and return a boolean indicating that the addition was successful or not.也很明显,因为这可以是列表最后的add()添加并返回 boolean 指示添加是否成功。

yet on the other hand the addLast() method adds on the last but its void method so it wont return you a boolean.但另一方面, addLast()方法添加了最后一个但它的 void 方法,因此它不会返回 boolean。

from javaDoc of the addlast()来自addlast() 的 javaDoc

Appends the specified element to the end of this list.将指定元素附加到此列表的末尾。

This method is equivalent to add(E).此方法等效于 add(E)。

from the javaDoc of the add()来自add() 的 javaDoc

Appends the specified element to the end of this list.将指定元素附加到此列表的末尾。

This method is equivalent to addLast(E).此方法等效于 addLast(E)。

but one might prefer to use add() for multiple reasons:但出于多种原因,人们可能更喜欢使用add()

  • as mentioned the return value.正如提到的返回值。
  • the add(int index, E element) : which will add a certain element to certain index. add(int index, E element) :它将某个元素添加到某个索引。

and from javaDoc of the add(int index, E element)并来自add(int index, E element)javaDoc

Inserts the specified element at the specified position in this list.在此列表中的指定 position 处插入指定元素。

Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).将当前位于该 position 的元素(如果有)和任何后续元素向右移动(将其索引加一)。

and this method will throw an这个方法会抛出一个

IndexOutOfBoundsException if the index is out of range (index < 0 || index > size()) IndexOutOfBoundsException如果索引超出范围 (index < 0 || index > size())

so thats being said all what you are doing is the following:所以这就是说你所做的一切如下:

demo.addLast(15);//your list now looks like this 15-> null

where 15 is the head and the tail in this case.在这种情况下, 15是头部和尾部。

demo.addFirst(1);/your list now looks like this 1->15->null

now you have 1 as the head/leading element and the 15 as tail/trailing element.现在你有1作为头部/前导元素和15作为尾部/尾随元素。

and finally for最后是

demo.add(10);//your list now looks like this 1->15->10->null.

for further informations here is how the addFirst and addLast works in the background:有关更多信息,这里是addFirstaddLast如何在后台工作:

addFirst will call the linkFirst addFirst将调用linkFirst

private void linkFirst(E e) {
    final Node<E> f = first;
    final Node<E> newNode = new Node<>(null, e, f);
    first = newNode;
    if (f == null)
        last = newNode;
    else
        f.prev = newNode;
    size++;
    modCount++;
}

addLast() will call the linkLast addLast()将调用linkLast

void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

I highly suggest that you do an implementation so you could build a better understanding of the whole concept.我强烈建议您进行实施,以便您可以更好地理解整个概念。 happy learning.快乐学习。

The explanation would be the following:解释如下:

When you do demo.addLast(15) considere your list is empty, so you just have [15] after that you did demo.addFirst(1) , according the definition it is going to insert at start, so you have [1, 15], at last you did demo.add(10), it insert at final so you have [1, 15, 10] , when you print it will be [1, 15, 10] .当你做demo.addLast(15)考虑你的列表是空的,所以你只有[15]之后你做了demo.addFirst(1) ,根据它将在开始时插入的定义,所以你有 [1, 15],最后你做了 demo.add(10),它在最后插入,所以你有[1, 15, 10] ,当你打印时它将是[1, 15, 10]

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

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