Java for loop use method

Source: Internet
Author: User

The For loop in Java has a--foreach notation (typically used only to traverse the entire array, without worrying about cross-border issues).

1.1) General wording:

Package Foreach.main.sn;public class foreach {public static void main (string[] args) {int[] arr = {1,2,3,4,5};for (int i = 0; i < arr.length; ++i) {System.out.println (arr[i]);}}
1.2) foreach notation: (Note the Tis a temporary variable value that is used to hold the array value that is currently traversed and cannot be traversed if the value of the array needs to be changed

Package Foreach.main.sn;public class foreach {public static void main (string[] args) {int[] arr = {1,2,3,4,5};for (int t: ARR) {System.out.println (t);}}}


2) Iterate through the two-dimensional array using foreach:

Package Foreach.main.sn;public class foreach {public static void main (string[] args) {int[][] arr2 = {{1,2,3},{4,5},{6,7,8 }};for (int t1[]: arr2) {for (int t2:t1) {System.out.print (t2);} System.out.println ();}}}



Original address: http://blog.csdn.net/qingdujun/article/details/40799561


Java for loop use method

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.