[Java Basics] foreach loop and javaforeach Loop

Source: Internet
Author: User

[Java Basics] foreach loop and javaforeach Loop

Starting with a small program:

1 class lesson6foreach 2 {3 public static void main (String [] args) 4 {5 int array [] = {2, 3, 1, 5, 4, 6 }; 6 7 for (int I = 0; I <array. length; I ++) // cyclically outputs the array element 8 {9 System. out. println (array [I]); 10} 11} 12}View Code

However, we do not care about the I value in the for loop. Therefore, after Java 5, foreach is used to enhance the for loop.
Format:

For (element type variable: array name or Iterable instance) // The Iterable instance teacher says no worries, okay, no matter first.

{

}

The preceding Code is as follows:

1 class lesson6foreach 2 {3 public static void main (String [] args) 4 {5 int array [] = {2, 3, 1, 5, 4, 6}; 6 7 for (int x: array) // cyclically output array element 8 {9 System. out. println (x); 10} 11} 12}View Code

What if I encounter a two-dimensional array? Here is an example of a two-dimensional array:

1 class lesson6foreach 2 {3 public static void main (String [] args) 4 {5 char [] [] charArray = 6 {7 {'put', 'meng ', 'hao', 'ran', 'zhi', 'guang ', 'ling'}, 8 {'st', 'people', 'west', 'loan ', 'huang', 'h', 'loud'}, 9 {'yay', 'hua', '3', 'month', 'under', 'yang ', 'status'}, 10 {'orphan ', 'fan', 'yuan', 'shad', 'bi', 'kong', 'do '}, 11 {'wei', '', 'Chang', 'jiang', 'day', 'inter', 'stream'} 12 }; 13 14 for (char [] x: charArray) // two-dimensional 15 {16 for (char y: x) 17 System. out. print (y); 18 System. out. println (); 19} 20} 21}View Code

The result is as follows:


Java foreach Loop

<%
String [] ss = {"", "yes ",""};
%>
<C: forEach items = "$ {requestScope. alllog}" var = "dis">
<Tr>
<Td height = "41" valign = "top">
<Input type = "checkbox" name = "checkbox" value = "$ {dis. loginId}"/> </td>
<Td valign = "top"> <c: out value = "$ {dis. loginId}"> </c: out> </td>
<Td valign = "top"> <c: out value = "$ {dis. userId}"> </c: out> </td>
<Td valign = "top"> <c: out value = "$ {dis. loginTime}"> </c: out> </td>
<Td valign = "top"> <c: out value = "$ {dis. loginUserIp}"> </c: out> </td>
<Td valign = "top"> <c: out value = "<% = ss [$ {dis. ifSuccess}] %> "> </c: out> </td>
<Td valign = "top"> <c: out value = "$ {dis. loginDesc}"> </c: out> </td>
</Tr>
</C: forEach>

Comparison and testing of foreach loop and for loop in java

In... foreach, the I in your int I: c is not the subscript of the array, but the code encoding of your char conversion.
You should replace it with char I: c. In this way, I outputs characters.

Public static void main (String [] args ){
String str = "hello world !!! ";
Char c [] = str. toCharArray ();
Int count = 0;
For (char I: c ){
Count ++;
System. out. println ("c [" + count + "] =" + I + "\ t ");

}
}

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.