java array loop

java array loop

In Java, there are several ways to loop through an array.

Here is an example of how to loop through an array using a traditional for loop:

int[] numbers = {1, 2, 3, 4, 5};

for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}
Source:w‮ttual.ww‬uri.com

This code will print the elements of the numbers array one by one, starting from the first element (index 0) and ending at the last element (index numbers.length - 1).

You can also use the enhanced for loop, also known as the for-each loop, to loop through an array:

for (int number : numbers) {
    System.out.println(number);
}

This code will also print the elements of the numbers array one by one.

Finally, you can use the forEach() method of the Arrays class to loop through an array:

import java.util.Arrays;

Arrays.stream(numbers).forEach(number -> System.out.println(number));

This code will also print the elements of the numbers array one by one.

All of these approaches can be used to loop through an array and perform some operation on each element of the array.

Created Time:2017-11-03 00:14:43  Author:lautturi