Opening Hours :7AM to 9PM
import java.util.Arrays; class PrintArrays { public void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } } public class Main { public static void main(String[] args) { PrintArrays pa=new PrintArrays(); int[] arr = {1, 2, 3, 4, 5}; pa.printArray(arr); } }This program uses a for-loop to iterate through the array and print each element.
class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println(Arrays.toString(arr)); } }You can also use the enhanced for loop which is also called the for-each loop to print the array elements.
class Main{ public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; for (int element : arr) { System.out.print(element + " "); } } }In all the examples above, the output will be: