Java learning day29-List collection

A, List and ArrayList

1.List represents an element orderly and repeatable set, the set of each element has its corresponding sequence index.

2.List allows repetitive elements, the collection can be accessed by the element at the specified index.

3.List default settings by adding an element of order of the elements of the index.

4.List the collection adds some methods to manipulate the index element in the collection.

Collection Interface <- (inherited) -List Interface <- (implement) -ArrayList class

Package day16; 

Import of java.util.ArrayList;
 Import java.util.List; 

public  class Test5 should be conducted {
     public  static  void main (String [] args) { 
        List <String> List = new new the ArrayList <String> (); 
        
        List.add ( "B"); // first, the subscript index is 0, List element is set according to the default order of addition of the element index. 
        List.add ( "D"); // index subscript. 1 
        List.add ( "C"); // index subscript 2 
        List.add ( "A"); // index subscript. 3 
        List.add ( " D "); // superscript 4 index, .List allowing duplicate elements
         
        System.out.
        System.out.println (List.get ( 2)); // List can access a collection of elements indexed by the specified position 
        
        List.add ( . 1, "F"); // target position data is inserted at the index finger 
        System .out.println (List); 
        
        
        List <String> L = new new the ArrayList <String> (); 
        l.add ( "123" ); 
        l.add ( "456" ); 
        
        list.addAll ( 2, L); / / target position set in the inserted index finger 
        
        System.out.println (List); 
        
        
        System.out.println (list.indexOf ( ; "D")) // the index of the specified element in obtaining the first occurrence of the collection standard 
        System.out.println (list.lastIndexOf ( "d") );//Get index index specified within the collection of the last occurrence 
        
        
        list.remove ( 2); // remove elements based on the specified index index 
        System.out.println (List); 
        
        
        list.set ( . 1, "FF") ; // the specified index index modification value 
        System.out.println (List); 
        
        
        // contains index of the interception of a set of elements forming a new starting position according to the target, the time taken, at the end of the index does not contain 
        List <String> list.subList subList = (2, 4); // get the element index in the subscript 2 <= && <4 
        System.out.println (subList); 
        
        
        System.out.println (list.size () ); // length set 
    } 
}

Print results:

 

Two, ArrayList and Vector

ArraryList and Vector are two typical implementation of the List interface, except that:

1.Vector is an old collection, not commonly recommended ArrayList.

2.ArrayList is not thread-safe, but Vector is thread-safe, but even in order to ensure collection of thread-safe List, is not recommended to use Vector.

 

Guess you like

Origin www.cnblogs.com/su-peng/p/12568949.html