public class Program {
public static void main(String[] args) {
// Create three-element String array.
String[] elements = {
"cat",
"dog",
"mouse" };
// Print element count and first element.
System.out.println(elements.length);
System.out.println(elements[0]);
// Loop over strings in the list with for-each loop.
for (String element : elements) {
System.out.println(element);
}
// Use a longer syntax form for creating the array.
String[] elements2 = new String[] {
"bird",
"fish",
"cow" };
System.out.println(elements2.length);
// Loop over string indexes with for-loop.
for (int i = 0; i < elements2.length; i++) {
System.out.println(elements2[i]);
}
}
}
3
cat
cat
dog
mouse
3
bird
fish
cow