Java - ArrayList use Demo

Three kinds of traversal

  • By traversing the iterator Iterator
  • By get (index) traversal
  • for loop iterates

ArrayList using Demo

package list;
import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListDemo {

    public static void main(String[] srgs){
         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 printing element traversal
          // first: traversing through the iterator 
         System.out.print ( "iterates over by:" ); 
         the iterator <Integer> = IT arrayList.iterator ();
          the while (it.hasNext ()) { 
             of System.out.print (it.next () + "" ); 
         } 
         System.out.println (); 

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

         // Third: for loops 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 newInteger [arrayList.size ()]; 
         arrayList.toArray (Integer1); 

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

         // add an element at the specified location 
         arrayList.add (2, 2 );
          // delete the element at the specified location 
         arrayList.remove (2 );    
          // delete the specified element 
         arrayList .remove ((Object). 3 );
          // Analyzing contains arrayList. 5 
         System.out.println ( "IS ArrayList the contains. 5:" + arrayList.contains (. 5 )); 

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

【note】

  1. java length property for arrays is to say, for example, declare an array you want to know the length of the array is used the length of this property.

  2. The length java () method is for a string String to say, if want to see the length of the string is used length () this method.

  3. The size .java () method is for a generic collection to say, if want to see this generic how many elements, it calls this method to view

Guess you like

Origin www.cnblogs.com/MWCloud/p/11329593.html