zoukankan      html  css  js  c++  java
  • Java自学笔记(25):【集合】 LinkedList

    List 的直接子类

    构造方法:

    LinkedList()  构建一个空列表

    LinkedList(Collection <? extends E> c)  构建一个包含指定Collection的元素列表

    常用方法演示

    import java.util.LinkedList;
    
    public class LinkedListDemo {
        
        public static void main (String[] args) {
            LinkedList<String> list = new LinkedList<String>();
            //add()
            list.add("1");
            list.add("2");
            list.add("3");
            traverse(list);
            
            list.addFirst("0");
            list.addLast("4");
            traverse(list);
            
            //print the head
            System.out.println(list.element());   //only print head
            traverse(list);    //not remove the head
            
            //get head and last
            System.out.println(list.getFirst()+" "+list.getLast());  //only print
            traverse(list);
            
            //offer()
            list.offer("5");    //add 5 
            traverse(list);
            list.offerFirst("-1");
            list.offerLast("6");
            traverse(list);
            
            //poll    //will remove the element
            System.out.println(list.pollFirst()+" "+list.pollLast());
            traverse(list);          //remove the element
            
            //pop
            System.out.println(list.pop());
            traverse(list);   //remove the head
            
            //push
            list.push("0");
            traverse(list);
            
            //remove()
            list.removeFirst();  //remove the head
            list.removeLast();   //remove the last
            traverse(list);
            list.remove();  //remove the head
            traverse(list);   
            list.remove(1);  //remove the element at 1
            traverse(list);
            
            //other
            list.add(1,"a");  //add "a" at 1
            traverse(list);
            list.removeFirstOccurrence("a");  //remove the first "a"
            traverse(list);
            list.removeLastOccurrence("4"); //remove the last 4
            traverse(list);
        }    
        public static void traverse(LinkedList<String> list) {
            //遍历
            for(int i=0;i<list.size();i++) {
                System.out.print(list.get(i)+" ");
            }
            System.out.println();
        }
    }
  • 相关阅读:
    JVM运行内存分配和回收
    关于评审--从思想到落地
    从浏览器或者Webview 中唤醒APP
    控制台的艺术(附原理实现)
    深入研究嵌入式web服务器的视频监控应用
    css中url的路径含义及使用
    epoll
    C++中智能指针的设计和使用
    wget命令的使用
    UPNP
  • 原文地址:https://www.cnblogs.com/tkj521Ya/p/11319494.html
Copyright © 2011-2022 走看看