java - collection - collection - ArrayList

An array of fixed length can not be extended, so now the general set

CLASSIFICATION :( where the package: java.util)

collection value is a value stored

map storage is the key for the key (disorder not repeat) - value (disorder repeat)

 

collection Category:

list: re-ordering

set: do not repeat disorder

 

list:

ArrayList,Vector,LinkedList

 

package List;

import java.util.ArrayList;

public class ArrayListTest {
    public static void main(String[] args){

        //ArrayList
        ArrayList arrayList1 = new ArrayList();//无参数
        ArrayList arrayList2 = new ArrayList(3);//容量
        arrayList2.add("a");
        arrayList2.add("b");
        arrayList2.add("c");
        ArrayList arrayList3 = new ArrayList(arrayList2);//collection set, 
        the ArrayList arrayList4 = new new the ArrayList <String> (arrayList3); // can define the type of internal elements arraylist ----- "generic 


        // the Add 
        showarray (arrayList4);
         // ABC 
        arrayList4.add (" D " ); 
        showarray (arrayList4); 
        // ABCD 
        arrayList4.add (2, "D"); // in index = 2 insertion position "D" 

        // Remove 
        showarray (arrayList4);
         // abdcd 
        arrayList4.remove (. 1 ) ; 
        showarray (arrayList4); 
        // ADCD 
        arrayList4.remove ( "D");   //Delete the first encounter d
         // If you want to delete a number of type int, you need to remove (new Integer (1)) ;, otherwise deleted objects is index = 1. 



        // Modify 
        showarray (arrayList4);
         // ACD 
        arrayList4.set (2, "E"); // in position 2 of the element to inde = e (cover), will return to old, or to prevent deleting the wrong backup 


        // Check 
        showarray (arrayList4);
         // ACE 
        System.out.println (arrayList4.get (2)); // get to inde = position of the element 2
         // E 

        // size 
        System.out.println (arrayList4 .size ()); // List length
         // . 3 

        // List can be directly output, rewriting toString
        System.out.println (arrayList4);
         // [A, C, E] 


        arrayList4.removeAll (arrayList2); // delete the arrayList4, arrayList2 [a, b, c ] elements contained 
        System.out.println (arrayList4) ;
         // [E] 

        arrayList4.add ( "a" ); 
        arrayList4.addAll (arrayList2); // add arrayList2 [a, b, c] to the elements contained arrayList4 may be repeated, added directly behind the 
        System. Out.println (arrayList4);
         // [E, A, A, B, C] 

        arrayList4.retainAll (arrayList2); // intersection of repeatable 
        System.out.println (arrayList4);
         // [A, A, b, c] 

        // delete all values
        // arrayList4.removeAll (arrayList4); 

        System.out.println (arrayList4.contains ( "A")); // determines whether comprise
         // to true 
        arrayList4.add ( "A" ); 
        System.out.println (arrayList4. the indexOf ( "a")); // first "a" to appear 
        System.out.println (arrayList4.lastIndexOf ( "a")); // the last position "a" appears
         // 0
         // . 4 


        String [] SARR = new new String [arrayList4.size ()]; 
        arrayList4.toArray (SARR);   // public <T> T [] toArray (T [] a) returns the value of the parameter, the element into the arrayLst parameters return 
        for (String s:sArr) {
            System.out.print(s + " ");
        }
        System.out.println();
        //a a b c a





    }

    private static void showArray(ArrayList arrayList){
        for(Object s:arrayList){
            System.out.print(s.toString() + " ");
        }
        System.out.println();
    }

}

 

Guess you like

Origin www.cnblogs.com/clamp7724/p/11627332.html