Several methods of using java ArrayList

Package java06; 

Import of java.util.ArrayList; 

/ * 
number of commonly used methods of ArrayList: 

public Boolean the Add (E E): Summary added to the collection element, and the same type of generic parameters, return value represents whether the addition of elements successfully 
Remarks : for the ArrayList collection is, add to add certain elements to be successful, it can not return values 
but for other collections it, add add actions are not necessarily successful 

public E get (int index) Gets an element from the collection, parameter index value the return value is the element position corresponding 

public E remove (int index) to remove elements from the collection, the parameter value of the index value, the return value is deleted element 

public int size (): Get the length of the set, the return value is set contains the number of elements 

* * / 
public  class DemoArrayListMethod {
     public  static  void main (String [] args) { 
        the ArrayList <String> List = new new the ArrayList <> ();
         Booleansuccess = list.add ( "Bob" ); 
        System.out.println ( "Success additive element:" Success +); // add elements success: to true 
        System.out.println (List); // [Bob] 

        List. the Add ( "Xiaogang" ); 
        List.add ( "red" ); 
        List.add ( "S" ); 
        List.add ( "large" ); 
        System.out.println (List); // [ Xiaoming, Xiaogang, red, trumpet, tuba] 

        String STR = List.get (0 ); 
        System.out.println (STR); // Bob 

        System.out.println (list.remove ( 2)); // red

        System.out.println (list.size ()); // . 4 

        System.out.println (List); // [Xiaoming, Xiaogang, trumpet, tuba] 
    } 
}
package java06;

import java.util.ArrayList;

public class DemoArrayListtrain {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("小明");
        list.add("腾讯");
        list.add("微信");
        list.add("口酒");
        list.add("喝水");
        list.add("吃饭");
        System.out.print(list);//[小明, 腾讯,Micro-channel, port wine, drink water, eat]
        System.out.println (); 

        for ( int I = 0; I <list.size (); I ++ ) { 
            System.out.println (List.get (I)); // Xiaoming Tencent micro channel port wine dinner drink 

        } 


    } 
}

 

Guess you like

Origin www.cnblogs.com/spp666/p/11706866.html