JAVA basis of the List interface

Personal understanding:

  list interface is a subclass of the Collection interface, it inherits all the methods of the Collection interface, but also has its unique way, but do not carry out any operation at the time of iteration. Keep in mind the four structures of data storage: stacks, queues, arrays, linked lists, and guide their differences and what is suitable for the scene. Of particular note is LinedList interface is empty is determined whether the determination of which element is empty, only when the (new) is defined as the set represents Null was not present, a null pointer exception. As for Vector collection, ArrayList instead recognize that it is its iterative method (Enumeration: Enumeration) is replaced by Iterator.

A, List Interface Description:

1, List interface is a subclass Collection interface;

2, which is an element of an ordered set of access (and not reverse the positive sequence, is how to take on how to deposit);

3, it is with a set index by indexing operation can be accurately set of elements (the index into the array is a reason);

4, there may be repeated in the collection of elements, element by the equals method to compare whether the repetitive elements;

5, there is used subclasses: ArrayList collection, set the LinkedList.

Two, LIst set of methods commonly used:

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

public  class Demo01 {
     public  static  void main (String [] args) { 
        List <String> List = new new the ArrayList <String> (); 
        List.add ( "123" ); 
        List.add ( "ABS" );
         // the specified position of the insertion 
        List.add (. 1, "456" );
         // delete the specified location element 
        String list.remove S = (. 1 ); 
        System.out.println ( "deleted elements:" + S);
         // replace elements in the specified position
        String str = list.set (0, "Bob" ); 
        System.out.println ( "alternative elements:" + STR);
         // iterate 
        for ( int I = 0; I <list.size (); I ++ ) { 
            System.out.println (List.get (I)); 
        } 
    } 
}

1, adding elements method

①, add (Object e): at the end of the collection, add the specified element

②, add (int index, Object e): After the shift to the collection at the specified index, add the specified elements, elements of the original sequence

Second, remove elements deleted

①, remove (Object e): specified element object, removed from the collection, return the value of the element to be deleted

②, remove (int index): the element at the specified index, removed from the collection, return the value of the element to be deleted

Third, the method of replaced elements

①, set (int index, Object e): The index of the specified element, replace the specified element is replaced before the element returns

Fourth, the query element method

①, get (int index): Gets the element at the specified index, and returns that element

Three, Iterator concurrent modification exception:

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

public  class Demo02 {
     public  static  void main (String [] args) { 
        List <String> ARR = new new the ArrayList <String> (); 
        arr.add ( "A" ); 
        arr.add ( "B" ); 
        arr.add ( "C" );
         // determines whether there is traversed b, any add a D
         // Get iterator object 
        iterator < String> IT = arr.iterator ();
         the while (it.hasNext ()) { 
            String S =it.next();
            if(s.equals("b")){
                arr.add("d");
            }
        }
    }
}

 

Java.util.ConcurrentModificationException will run!

Solution: When iteration, do not use a set of operating elements;

  Then you want the elements supposed to operate in the iteration? Operating elements by ListIterator iterator is possible, ListIterator appeared to address the error condition using an Iterator process may occur.

Four, List data structure storing a collection:

Common structure data stored: stack, queue, array, linked list.

1, the stack:

Last-out (inlet stack, the stack are outlet tip position); memory element is pushed onto the stack, popping element is taken.

2, queue:

FIFO (queue entry, half and an outlet side).

3, the array (invariable length):

Finding elements quickly (by index), add or delete elements slow (need to create a new array).

4, list (among a plurality of nodes connected via address):

Find elements slow (required by the connected nodes, a look back), add or delete elements fast

Five, ArrayList Collection:

ArrayList set of stored data structure is an array of structures (for use when the query);

Six, LinkedList collection:

LinedList data storage structure is a set of linked list structure (for deletions when used);

Import java.util.LinkedList; 

public  class Demo03 {
     public  static  void main (String [] args) { 
        the LinkedList <String> ARR = new new the LinkedList <String> (); 
        arr.addFirst ( "A" ); 
        arr.addFirst ( " B " ); 
        arr.addLast ( " C " ); 
        arr.addLast ( " D " );
         / * for (String S: ARR) { 
            System.out.println (S); 
        } * / 
        // Get the collection of an element 
        System.out.println ( "as the first element in the set:" +arr.getFirst ());
         // Gets the last 
        System.out.println ( "the last element in the collection is:" + arr.getLast ());
         // Delete the first 
        arr.removeFirst (); 
        arr.removeFirst (); 
        // delete the last 
        arr.removeLast ();
         for (String S: arr) { 
            System.out.println (S); 
        } 
        // the collection does not print empty when 
        IF (! arr.isEmpty ()) { 
            System.out.println ( "the set is not empty" ); 
        } 
    } 
}

Seven, Vector collection

    Vector set of stored data structure is an array of structures, the first set is provided in the JDK. Vector provides a unique way out is to enumerate Enumeration, it is actually an early iterators. Iterator interface functions and features of this interface Enumeration is similar.

  Vector collection has been replaced ArrayList. Enumeration Enumeration has been replaced by an iterator Iterator.

 

Guess you like

Origin www.cnblogs.com/21-forever/p/10942525.html