Java Foundation---Using the foreach operation Array (33)

Source: Internet
Author: User

Using the foreach Action array

foreach is not a keyword in Java, it is a special simplified version of A for statement, which is simpler and easier to iterate through arrays and collections. In terms of the literal meaning of foreach, which means "for each", how do you use a foreach statement?


650) this.width=650; "src=" "/>

We use the for and foreach statements to iterate through the array, respectively.

650) this.width=650; "src=" "/>

Operation Result:

650) this.width=650; "src=" "/>

It's easy to see the foreach!!

Leave a question to everyone: what if you want to get the subscript for an array element in a foreach statement??


Import Java.util.Arrays;

public class HelloWorld {

public static void Main (string[] args) {

Define an integer array and save the score information
Int[] Scores = {89, 72, 64, 58, 93};

Sorting an array of arrays classes
Arrays.sort (scores);

Iterating through the elements in the output array using foreach
for (int score:scores) {

Operation Result:


This article is from "Ghost" blog, please make sure to keep this source

Java Foundation---Using the foreach operation Array (33)

Related Article

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: 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.