List collection in Java and its traversal detailed _java

Source: Internet
Author: User

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

①collection (The collection framework appears as a JDK1.2 version)

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

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

③arraylist: The underlying data structures use an array structure,

Features: Query speed quickly, but added or deleted slightly slower. Thread is not synchronized

LinkedList: The underlying use is the linked list data structure.

Features: Adding and deleting speed quickly, query slightly slow.

Vector: (JDK1.0 version appears) the underlying is the array data structure, thread synchronization. was replaced by ArrayList. (No more)

2. List two ways to traverse:

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 iterative traversal ... ");
    Listiterator<string> it = List.listiterator ();
    while (It.hasnext ()) {
      Object obj = It.next ();
      System.out.println (obj);}}


Effect Chart:

3. Use linklist to simulate a stack or queue data structure. That is: stack: Advanced out; queue: Advanced First Out

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 ();//Advanced First out---to be replaced by Advanced, 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 ());
    }
}}

Effect Chart:

Above for advanced first out, if you want to replace the advanced, then according to the code written in the change can be

The above mentioned is the entire content of this article, I hope you can enjoy.

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.