Analysis of Java LinkedList collection function examples

Since the underlying data structure of LinkedList is a linked list, there are some unique functions corresponding to the set from the linked list.

Framework code:

public class LinkedListDemo {
  public static void main(String[] args) {
    //创建集合对象
    LinkedList<String> linkedList = new LinkedList<String>();

    //添加元素
    linkedList.add("hello");
    linkedList.add("world");
    linkedList.add("java");

    //输出集合
    System.out.println(linkedList);
  }
}

Specific method implementation code:

     //public void addFirst(E e): 在该列表开头插入指定的元素
    linkedList.addFirst("javase"); //[javase, hello, world, java]

    //public void addLast(E e): 将指定的元素追加到此列表的末尾
    linkedList.addLast("javaee"); //[javase, hello, world, java, javaee]
//public E getFirst(): 返回此列表中的第一个元素
    System.out.println(linkedList.getFirst());
    /*
      hello
      [hello, world, java]
     */

//public E getLast(): 返回此列表中的最后一个元素
    System.out.println(linkedList.getLast());
    /*
      java
      [hello, world, java]
     */

//public E removeFirst(): 从此列表中删除并返回第一个元素
    System.out.println(linkedList.removeFirst());
    /*
      hello
      [world, java]
     */

//public E removeLast() : 从此列表中删除并返回最后一个元素
    System.out.println(linkedList.removeLast());
    /*
      java
      [hello, world]
     */

Some high-frequency interview questions collected in the latest 2020 (all organized into documents), there are many dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations, as well as detailed learning plans, interviews Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/113924471