A typical instance of an array is traversed in Java using the while statement _java

Source: Internet
Author: User
public class Birdarray {public 
  static void Main (String args[]) { 
    string[] str = new string[]{"Sparrow", "Eagle", "dove", "with", "Lark", "Peacock", "parrot", "Red-crowned Crane"}; 
    int index =0;      CREATE INDEX variable 
    System.out.println ("There are a lot of birds in the park, species include:"); 
    while (index<str.length) {//traversal array 
      System.out.println (str[index++]);//self-indexed value 
    } 
  } 


Output:

Run: 
There are many birds in the park, species include: 
sparrow 
Eagle 
dove 
with 
Lark Bird 
Peacock 
Parrot 
Red-crowned crane 

Summarize:

Creates an index variable index, which specifies the subscript for an array, and as the index increments, the while loop iterates through each element and outputs it to the console. Pay attention to the difference between ++index and index++.
++index: The value of index is incremented, and then the incremented value is used.
index++: First Use the value of index, and then increment the value of the variable.

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.