Various traversal outputs (classic version)----Java Basics Summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.