LinkedList example and linkedlist example

Source: Internet
Author: User

LinkedList example and linkedlist example

Package com. smbea. demo; import java. util. iterator; import java. util. using list; import java. util. list; import java. util. listIterator;/*** 1. inherited from the interface List, Deque, Cloneable, and Serializable; * 2. implemented from the class AbstractSequentialList; * 3. inherited from the abstract class AbstractSequentialList, therefore, the elements are ordered. * 4. The addition and deletion operations are slow because the elements need to be moved. * 5. the query operation uses indexes, so the query is faster; * 6. Non-thread-safe-lightweight; * 7. Because the Deque interface is implemented, not only can elements be queried from the past to the future, but also elements can be queried from the back to the front; * 8. Because the interface Cloneable is implemented, you can copy the parameter list; * 9. Because the interface Serializable is implemented, therefore, it is possible to serialize * @ author hapday */public class into listdemo {public static void main (String [] args) {elementRepeatableTest (); elementSequentialTest ();} /*** verify [repeated elements] */public static void elementRepeatableTest () {List <String> chineseList = new external List <String> (); chineseList. add ("A"); chineseList. add ("B"); chineseList. add ("B"); chineseList. add ("C"); chineseList. add ("ding"); List <String> engishlist = new sort List <String> (); engishlist. add ("A"); englishList. add ("B"); englishList. add ("B"); englishList. add ("C"); englishList. add ("D"); ListIterator <String> chineseIterator = chineseList. listIterator (); // supports reverse iteration of Iterator <String> engishiterator = engishlist. iterator (); // only supports sequential iteration of System. out. println ("*** sequential output ***"); while (chineseIterator. hasNext () {System. out. print (chineseIterator. next () + "-");} System. out. println ("\ n *** reverse output ***"); while (chineseIterator. hasPrevious () {System. out. print (chineseIterator. previous () + "-");} System. out. println ("\ n **** the elements inserted in the englishList ***" at an interval between chineseList); while (englishIterator. hasNext () {if (chineseIterator. hasNext () {chineseIterator. next ();} chineseIterator. add (englishIterator. next (); // Insert the element in the englishList at the interval of chineseList} System. out. println (chineseList); engishiterator = engishlist. iterator (); System. out. println ("\ n *** englishList deletes the previous element ***" every two elements); while (englishIterator. hasNext () {englishIterator. next (); if (englishIterator. hasNext () {englishIterator. next (); englishIterator. the remove (); // remove () method and next () method appear in pairs, and each deletion is the previous element of next ()} System. out. println (englishList); System. out. println ("\ n *** remove the englishList element from chineseList ***"); chineseList. removeAll (englishList); System. out. println (chineseList);}/*** [element ordered] */public static void elementSequentialTest () {List <Integer> list = new sequence List <Integer> (); for (int index = 1; index <= 100000; index ++) {list. add (index); // add the 0.1 million element} boolean exists = true; // whether it exists: true-Yes; false-int current = 0; // The current element int previous = 0; // The previous element Iterator <Integer> iterator = list. iterator (); while (iterator. hasNext () {current = iterator. next (); if (current <previous) {exists = false;} previous = current;} System. out. println ("\ n *** elements are ordered ***"); System. out. print ("sequential:" + exists );}}

  

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.