Java-for iterating through an array

Source: Internet
Author: User

Format:
For (data type variable name: Array or collection) {
SOP (variable);
}

 Public Static void function_1 () {        //for the object array traversal, can I invoke the method of the object        string[] str = {"abc", "Itcast", "CN" };          for (String s:str) {          System.out.println (s.length ());        }      }

Implementing a For loop, iterating over an array
* Benefits: Less code, convenient for container traversal
* Disadvantages: No index, cannot manipulate elements inside the container

 Public Static void function () {        int[] arr = {3,1,9,0};          for (int  i:arr) {          System.out.println (i+1);        }        System.out.println (arr[0]);      }

Enhanced for loop Traversal collection

 Public Static void function_2 () {          ArrayListnew arraylist<person>();          Array.add (new person ("a", +));          Array.add (new person ("B", ten));            for (person P:array) {            System.out.println (p); // System.out.println (p.tostring ());           }        }

Java-for iterating through an array

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.