Several ways of writing for loops (change)

Source: Internet
Author: User
Package Com.test.For_Each;

Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;

public class Fortest
{
public static void Main (String args[])
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};

/**
* New Style
*/

for (int a:arr)
System.out.println (a);

/**
* Old Style
*/
for (int i = 0; i < arr.length; i++)
System.out.println (Arr[i]);


String arr2[] = {"Good", "stream", "Oh", "!!"};
for (String a2:arr2)
SYSTEM.OUT.PRINTLN (A2);


int arr3[][] = {{1,2,3},{4,5,6,},{7,8,9}};
for (int a31[]: ARR3)
{
for (int a32:a31)
{
System.out.println (A32);
}
System.out.println ();
}


list<string> list = new arraylist<string> ();
List.add ("good");
List.add ("Flow");
List.add ("Oh");
List.add ("!!");
/**
* Traversal based on collection class length
*/
for (int i=0;i<list.size (); i++)
{
System.out.println (List.get (i));
}


/**
* Based on iterator traversal
*/
for (Iterator i = List.iterator (); I.hasnext ();)
{
System.out.println (I.next ());
}

/**
* Based on the new For-each traversal
*/
for (String element:list)
SYSTEM.OUT.PRINTLN (Element);
}
}

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.