Traversal methods of collection

Using enhanced for loop iterates over the collection and

Package com.aff.coll;
 Import of java.util.ArrayList;
 Import java.util.Collection;
 Import java.util.Date;
 Import the java.util.Iterator;
 Import org.junit.Test;
 public  class testfor { 

    // face questions 
    @Test
     public  void Test3 () { 
        String [] STR = new new String [] { "AA", "BB", "the CC" };
         for (String s: STR) { 
            s = "mm"; // where s local variables is newly defined, its modified value itself does not affect the str 
            System.out.println (S); // mm mm mm 
        }
        for ( int I = 0; I <str.length; I ++ ) { 
            System.out.println (STR [I]); // the CC AA BB 
        } 
    } 

    // enhanced for-loop set traversal may take iterate 
    @Test
     public  void Test () { 
        Collection Coll = new new the ArrayList (); 
        coll.add ( 123 ); 
        coll.add ( "AA" ); 
        coll.add ( new new a Date ()); 
        coll.add ( "BB" ) ; 
        coll.add ( new new the Person ( "mm", 13 is ));
         for (Object I: Coll) {
            System.out.println (I); 
        } 
    } 

    // enhanced for-loop set traversal may take iterate 
    @Test
     public  void test1 () { 
        String [] STR = new new String [] { "AA", "BB "," DD " };
         for (String S: STR) { 
            System.out.println (S); 
        } 
    } 
    
    // using an iterator iterator traversal set 
    @Test
     public  void test0 () { 
        collection Coll = new new the ArrayList ( ); 
        coll.add ( 123 ); 
        coll.add ( "AA" );
        coll.add(new Date());
        coll.add("BB");
        coll.add(new Person("mm", 13));
        
        Iterator iterator = coll.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/afangfang/p/12585170.html