Class ArrayList (set)

About ArrayList class

is java.util.ArrayList achieve a variable-size array of data storage elements including called. Such methods provide for internal storage of the operating elements. ArrayList can continue to add elements, its size also automatically increase. ArrayList object can not be stored basic types, the type of data stored reference only.

ArrayList using the steps

View class
  • java.util.ArrayList <E>: after introduction of such import need to make use.
  • <E>, represents one of the specified data type, called generic. E, taken from Element (elements) of the first letter. E appears in place of, we use a reference data type can replace it, which means we will store reference type elements. After JDK 7, the right angle brackets within the generic may be left blank, but <> still write.
View constructor
  • public ArrayList (): a content is configured to empty set.
View member method
  • public boolean add (E e): the specified element to the end of the collection.
  • public E remove (int index): Removes the element at the location specified in this collection. Returns the element to be deleted
  • public E get (int index): Returns the collection element at the specified location. Returns the element obtained.
  • public int size (): Returns the number of elements in this set. Traversing a collection index range may be controlled, to prevent cross-border.
Package demo04; 

Import of java.util.ArrayList; 

public  class Demo01ArrayList {
     public  static  void main (String [] args) {
         // create the collection object, generics String 
        the ArrayList the arrayList = <String> new new the ArrayList <> ();
         // add elements 
        arrayList.add ( "Joe Smith" ); 
        arrayList.add ( "John Doe" ); 
        arrayList.add ( "Wang Wu" );
         // public E GET (int index): returns the element at the specified index 
        System. Out.println ( "GET:" + arrayList.get (0)); // GET: Zhang
        System.out.println ( "GET:" + arrayList.get (1)); // GET: John Doe 
        System.out.println ( "GET:" + arrayList.get (2)); // GET: Wang Wu
         // public int size (): returns the number of elements in the set 
        System.out.println ( "size:" + arrayList.size ()); // . 3
         // public E remove (int index): delete the specified index at the element, the element returns deleted 
        System.out.println ( "remove:" + arrayList.remove (0)); // remove: Zhang
         // traverse output 
        for ( int I = 0; I <arrayList.size (); I ++ ) { 
            of System.out.print (arrayList.get (I)); // John Doe Wangwu 
        } 
 
    }
}

How to store basic data types

ArrayList object can not be stored basic types, the type of data stored reference only. Similar <int> can not write, the memory type corresponding to the basic data type of packaging are possible. Therefore, you want to store the basic data types, data type in <>, which must be written to the corresponding packaging

Precautions:

From the start unpacking and packing JDK1.5 support the automatic execution

  • Autoboxing   converted to the corresponding elementary data type packaging (basic data types ---> packaging)
  • Automatic unboxing   packaging into corresponding basic data types (packaging ---> basic data types)
  • Only wrapper class Integer and Character require special memory, but other basic types can be capitalized.
Package demo04; 

Import of java.util.ArrayList; 

public  class Demo02ArrayList { 

    public  static  void main (String [] args) {
         // create a storage base data type set 
        the ArrayList <Integer> = List new new the ArrayList <Integer> ();
         // added to the set time autoboxing 
        List.add (. 1 ); 
        List.add ( 2 ); 
        List.add ( . 3 ); 
        List.add ( . 4 );
         // automatically unpacking 
        int I = List.get (. 1 ) ; 
        System.out.println (I); //2
    }
}

Gets a collection method 

Get all the even-numbered elements of the definition set of methods (the ArrayList type as the return value), randomly generated set elements range 1-1000
Package demo04; 

Import of java.util.ArrayList;
 Import java.util.Random; 

public  class Demo03ArrayList {
     public  static  void main (String [] args) {
         // create objects Random 
        Random Random = new new Random ();
         // create ArrayList object 
        the ArrayList <Integer> = List new new the ArrayList <> ();
         // add to the set of random numbers 
        for ( int I = 0; I <20 is; I ++ ) {
             int R & lt random.nextInt = (1000) +. 1 ; 
            List.add ( R & lt);
        } 
        // call the method of the even-numbered set of 
        the ArrayList <Integer> = the arrayList getArrayList (List); 
        System.out.println (the arrayList); // [132, 882, 320., 548, 424, 708, 446, 404, 104, 364 , 976] 
    } 

    public  static the ArrayList <Integer> getArrayList (the ArrayList <Integer> List) {
         // create a set of small, to preserve the even 
        the ArrayList <Integer> = smallList new new the ArrayList <> ();
         // iterate List 
        for ( int I = 0; I <list.size (); I ++ ) {
             // Get element 
            Integer NUM = List.get (I);
            // determination is even added to the smaller collection 
            IF (NUM% 2 == 0 ) { 
                smallList.add (NUM); 
            } 
        } 
        // Returns a collection of small 
        return smallList; 
    } 
}

 

 

Guess you like

Origin www.cnblogs.com/wurengen/p/10959534.html