ArrayList Classic Demo

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListDemo {

    public static void main(String[] args){
         ArrayList<Integer> arrayList = new ArrayList<Integer>();

         System.out.printf("Before add:arrayList.size() = %d\n",arrayList.size());

         arrayList.add(1);
         arrayList.add(3);
         arrayList.add(5);
         arrayList.add(7);
         arrayList.add(9);
         System.out.printf ( "the After the Add: arrayList.size () =% D \ n-" , arrayList.size ()); 

         System.out.println ( "Elements of the arrayList Printing" );
          // three kinds of printing traversal element
          // first: traversing through the iterator 
         System.out.print ( "iterates over by:" ); 
         the iterator <Integer> = IT arrayList.iterator ();
          the while (it.hasNext ()) { 
             the System.out .print (it.next () + "" ); 
         } 
         System.out.println (); 

         // second: by traversing the index value 
         System.out.print ( "by traversing the index value:" );
         for(int0 = I; I <arrayList.size (); I ++ ) { 
             of System.out.print (arrayList.get (I) + "" ); 
         } 
         System.out.println (); 

         // Third: for looping through 
         System.out.print ( "for loop iterates:" );
          for (Integer Number: the arrayList) { 
             of System.out.print (Number + "" ); 
         } 

         // toArray usage
          // first embodiment (the most common) 
         Integer [] = Integer arrayList.toArray ( new new Integer [0 ]); 

         // second embodiment (easily understood) 
         Integer [] = Integer1 new new  Integer [arrayList.size ()];
         arrayList.toArray (Integer1);

         // throws an exception, java does not support the downward transition
          // Integer [] = Integer2 new new Integer [arrayList.size ()];
          // Integer2 arrayList.toArray = (); 
         System.out.println (); 

         // in specified location additive element 
         arrayList.add (2,2 & );
          // delete the element on the specified position 
         arrayList.remove (2 );    
          // delete the specified element 
         arrayList.remove ((Object). 3 );
          // Analyzing arrayList contains 5 
         System.out.println ( "iS ArrayList the contains. 5:" + arrayList.contains (. 5 )); 

         // Clear ArrayList 
         arrayList.clear ();
          // determines whether the air ArrayList
         System.out.println("ArrayList is empty: " + arrayList.isEmpty());
    }
}

 

Guess you like

Origin www.cnblogs.com/massionter/p/10968472.html