A list collection in Java and its traversal

Source: Internet
Author: User

1. First list<e> set inheritance with Collection<e>, is an interface.

①collection (The collection frame is the JDK1.2 version appears)

②list: Is ordered, the elements can be duplicated, thinking that the collection system has an index.

The ArrayList and LinkedList classes that implement this interface are often used

③arraylist: The underlying data structure uses the array structure,

Features: Fast query, but slightly slower additions and deletions. Thread is out of sync

LinkedList: The underlying uses a linked list data structure.

Features: Deletion speed quickly, query slightly slow.

Vector: (The JDK1.0 version appears) The bottom layer is the array data structure, thread synchronization. Replaced by the ArrayList. (no longer necessary.)

2. Two ways to traverse the list:

public class Demo {public  static void Main (string[] args) {    arraylist<string> list = new Arraylist<strin G> ();    List.add ("A");    List.add ("B");    List.add ("C");         System.out.println ("... The first traversal method: for traversal ... ");    for (Object li:list) {      System.out.println (LI);    }         System.out.println ("... The second way of traversal: Listiterator iterate ... ");    Listiterator<string> it = List.listiterator ();    while (It.hasnext ()) {      Object obj = It.next ();      System.out.println (obj);}}}  

3. Use linklist to simulate a stack or queue data structure. i.e.: Stack: Advanced back out; queue: FIFO

Class duilie{  private linkedlist<object> link;  Duilie () {    link = new linkedlist<object> ();  }  public void Myadd (Object obj) {    link.addfirst (obj);   }  Public Object MyGet () {    return link.removelast ();//FIFO---to change to advanced, turn removelast () to Removefirst ()  }  public Boolean isNull () {    return link.isempty ();  }} public class Demo2 {public  static void Main (string[] args) {    Duilie dl = new Duilie ();    Dl.myadd ("Java01");    Dl.myadd ("Java02");    Dl.myadd ("java03");    Dl.myadd ("Java04");    while (!dl.isnull ()) {      System.out.println (Dl.myget ());}}}  

The above is first-out, if you want to change the advanced, then according to the code to write a change on it.

The above mentioned is the whole content of this article, I hope you can like.

A list collection in Java and its traversal

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.