Write a for loop to output the elements an array

Assuming that the numbers are double:

for (double number : MyArray) {
    System.out.println(number);
}

So a complete working code would be:

public class IterateArray {

    public static void main(String[] args) {
        double[] MyArray = {2.3, 3.5, 4.5, 5.0, 3.5, 0.2};

        for (double number : MyArray) {
            System.out.println(number);
        }
    }
}

Leave a Comment