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.