• 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();
        }
    }
  • 相关阅读:
    MySQL语法
    SQL必知必会
    大话设计模式
    软件工程
    myeclipse中git的使用
    提取文件中图片标签
    pandas 分析各地区男女的分布情况
    提取图片标签
    Requwsts+正则表达式爬取猫眼电影Top100
    PyCharm激活码
  • 原文地址:https://www.cnblogs.com/tkj521Ya/p/11319494.html
Copyright © 2020-2023  润新知