how-to

Arrays

How to go through an array

VARIANT 1

Easy for loop:

String[] array = new String[3];

array[0] = "One";
array[1] = "Two";
array[2] = "Three";

for (int i = 0; i < array.length; i++ )
  System.out.println( array[i] );

for ( String s : array )
  System.out.println( s );

The output is:

One
Two
Three

VARIANT 2

Using enhanced for loop version:

String[] array = new String[3];

array[0] = "One";
array[1] = "Two";
array[2] = "Three";

for ( String item : array )
  System.out.println( item );

In this enhanced for loop version are all items in an array filled into variable of a given type - for every array item is filled variable item again and again.

The output is:

One
Two
Three