Java Collection LinkedList Common instance operation, example description

Source: Internet
Author: User

I. LinkedList COMMON operations

Package list related;/** * LinkedList link list * LinkedList: The underlying data structure used by the table. Features: Fast and easy to delete, query speed slightly slower; */import Java.util.iterator;import java.util.linkedlist;class linkedlist_test {public static void Mai        N (string[] args) {LinkedList linkedlist = new LinkedList ();        LinkedList linkedlist1 = new LinkedList ();        Linkedlist1.add ("what");//Linkedlist1.add ("Zhangj");        /** */System.out.println ("------------new Operation------------");        add element Linkedlist.add at the tail ("what");        Linkedlist.add ("?");        add element Linkedlist.add (1, "Now") at the specified location;        System.out.println (LinkedList);        add Element Linkedlist.addfirst ("one") at the top of the list;        add Element Linkedlist.addlast ("last") at the end of the list;        In the specified add element linkedlist.addall (1,LINKEDLIST1);        System.out.println (LinkedList);        /** */System.out.println ("------------query Operation------------");        System.out.println ("LinkedList list is:" +linkedlist); Get first element Object obj1 = Linkedlist.getfirSt ();        Gets the element at the specified position Object obj2 = Linkedlist.get (2);        Gets the trailing element Object obj3 = Linkedlist.getlast ();        Query the length of the list of links (number of elements) int a1= linkedlist.size ();        System.out.println ("The first element is:" +obj1+ "; the third element is:" +obj2+ "; the trailing element is:" +obj3+ "; the number of internal elements is:" +A1);        /** Modify */System.out.println ("------------Modify Operation------------");        System.out.println ("The list of previously modified lists is:" +linkedlist);        Linkedlist.set (1, "what");        SYSTEM.OUT.PRINTLN ("Modified list of linked lists:" +linkedlist);            Traversal list//through while mode to traverse Iterator it1 = Linkedlist.listiterator ();//while (It1.hasnext ()) {// System.out.println ("While:" +it1.next ()),///}///For looping through for (; It1.hasnext ();) {System.        Out.println ("For:" +it1.next ()); /** Delete * When deleting with remove, the deleted object is also removed from the delete operation. * Remove (intdex int) When the delete operation occurs, the index throws an exception if it exceeds the corner label boundary Inde        Xoutofboundsexception * However, when the poll type is used, the index will return NULL if it exceeds the corner label boundary * */System.out.println ("------------delete operation------------");        System.out.println ("Delete start list of linked lists:" +linkedlist);        Removefirstoccurrence, the first occurrence of the specified element in LinkedList linkedlist.add (1, "Now"); SYSTEM.OUT.PRINTLN ("-----begin-------------" + "linkedlist=" +linkedlist);//Linkedlist.removefirstoccurrence ("Now        ");        Removelastoccurrence, the first occurrence linkedlist.removelastoccurrence ("Now") of the specified element in LinkedList;        SYSTEM.OUT.PRINTLN ("-----End---------------" + "linkedlist=" +linkedlist);        Deletes the specified element object, the specified element is deleted, no specified element is not manipulated, and the return value is a Boolean type Object obj5 = Linkedlist.remove ("?");        SYSTEM.OUT.PRINTLN ("The element for deleting list of linked lists is:" +obj5);        Deletes an object at the specified index location linkedlist.remove (0);        The first element is deleted by index, which is equivalent to Linkedlist.remove (0);        Linkedlist.removefirst ();        Delete trailing element by index Object O = Linkedlist.removelast ();        System.out.println (O);        Delete all elements (empty list) linkedlist.clear ();        Remove and poll removal method comparison//Linkedlist.remove (9); Object Obj9=linkedlist.poll ();        System.out.println ("obj9=" +obj9); System.out.println ("End of the list of linked lists:" +linkedlist);//Other methods Introduction, not description//E-PEEK ()//Retrieve but do not delete the header of this list (the first element). E Peekfirst ()//retrieves the first element of this list without deleting it, and returns null if the list is empty. E peeklast ()//retrieves the last element of this list without deleting it, and returns null if the list is empty. E poll ()//retrieves and deletes the header of this list (the first element). E Pollfirst ()//retrieves and deletes the first element of this list, and returns null if this list is empty. E polllast ()//retrieves and deletes the last element of this list, and returns null if this list is empty.    E pop ()//The stack represented by this list pops an element. }}

Two. An in-practice operation
Concept Simple Description:
Stack: The element is advanced, similar to the Water cup filled with water;
Queue: Elemental first out, similar to water pipes;
Requirement: A functional container that implements a type stack heap or queue
1. Encapsulating a function container of a similar type stack or queue

import java.util.LinkedList;public class linkedlist_test1 {    /*实现类似栈堆或队列功能的容器*/    private LinkedList linkedlist;    //构造函数,一开始就生成一个链表队列linkedlist    linkedlist_test1(){        linkedlist = new LinkedList();    }    //定义向链表队列添加元素的功能    public void addElement(Object obj){        linkedlist.add(obj);    }    //定义一个类似栈堆功能(先进后出)    public Object ZhanDui(){        return linkedlist.removeFirst();    }    //定义一个类似队列功能(先进先出)    public Object DuiLie(){        return linkedlist.removeLast();    }    //定义一个判断linkedlsit是否为空的功能    public boolean isNull(){        return  linkedlist.isEmpty();    }

2. Call the Encapsulated class

public class Tset{public static void main(String[] args){        linkedlist_test1 test = new linkedlist_test1();        String array[] = {"what","you","want"};        test.addElement(1);        test.addElement("you");        test.addElement("and");        test.addElement(array);        //栈堆功能//        while (!test.isNull()){//            System.out.println(test.DuiLie());//        }        //队列功能        for(;!test.isNull();){            System.out.println(test.ZhanDui());        }    }}

Java Collection LinkedList Common instance operation, example description

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.