Foreword: About a total of 3 in the traversal output mode, very early I want to tidy up, helpless has not taken time, respectively, is the traditional for loop traversal, iterator Iterator,foreach, this time I pass the test code, test a bit.
First with a sketch, probably an impression:, although the picture is ugly, but all is dry.
This is my test code, I use the test method to write:
1 PackageCom.mon11.day11;2 3 Import Staticorg.junit.assert.*;4 5 Importjava.util.ArrayList;6 ImportJava.util.Iterator;7 Importjava.util.List;8 9 Importorg.junit.Test;Ten One /** A * Class Description: Three kinds of traversal output understanding - * @authorAuthor: Chenyanlong - * @versionCreation Date: November 11, 2017 the */ - Public classDemo1 { - - //1. Traditional for loop traversal output, + @Test - Public voidtest1 () { +System.out.println ("1. Traditional for loop traversal output-----------------"); A int[] arrays = {12, 34, 56 }; at for(inti = 0; i < arrays.length; i++) { - System.out.println (Arrays[i]); - } - } - - //2. Iterator traversal output iterator in @Test - Public voidtest2 () { to +List arrays =NewArrayList (); -Arrays.add ("21");//At first I was using an integer type, which was always incorrect, and if it was a string type, it would be correct. theArrays.add ("43"); *Arrays.add ("65"); $ Panax NotoginsengSYSTEM.OUT.PRINTLN ("2.1 Iterator for Loop------------------"); -SYSTEM.OUT.PRINTLN ("2.1------//This way, I have been thinking, until now still do not understand------------"); the for(Iterator a =arrays.iterator (); A.hasnext ();) { +String array1 =(String) A.next (); A System.out.println (array1); the } +System.out.println ("------------------"); - $System.out.println ("2.2 Iterator for While Loop------------------"); $Iterator B =arrays.iterator (); - while(B.hasnext ()) { -String array2 =(String) B.next (); the System.out.println (array2); - }Wuyi } the - //3.foreach loop traversal output, Wu @Test - Public voidtest3 () { About int[] arrays = {12, 34, 56 }; $System.out.println ("3.foreach loop traversal output-------------------"); - for(intr:arrays) { - System.out.println (r); - } A } +}The effect of running:
Various traversal outputs (classic version)----Java Basics Summary