How to create a linked list in java?

This question already has an answer here:

  • Initialization of an ArrayList in one line 32 answers

  • 这就是列表中的指针在内部看起来如何add其实际add到列表中,您需要这样做:

    List<String> s = new LinkedList<>(); 
    
    s.add("a"); 
    s.add("b");
    s.add("c");
    s.add("d");
    

    Take a look at this answer.

    LinkedList<String> list = new LinkedList<>();
    list.add("a");
    list.add("b");
    list.add("c");
    list.add("d");
    

    If you really want it on one line:

    LinkedList<String> list = new LinkedList<>(Arrays.asList("a","b","c","d"));
    

    Though that does have a performance overhead.


    你可以这样做:

    LinkedList<String> linkedList = new LinkedList<String>();
        linkedList.add("a");
        linkedList.add("b");
        linkedList.add("c");
        linkedList.add("d");
    
    链接地址: http://www.djcxy.com/p/27716.html

    上一篇: 单身收集到多个对象收集

    下一篇: 如何在java中创建链表?