java array print

java array print

To print the elements of an array in Java, you can use a loop to iterate over the array and print each element.

Here is an example of how to print the elements of 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]);
}
‮ww:ecruoS‬w.lautturi.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 print the elements of 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 print the elements of 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 print the elements of an array.

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