Java program to print an array
figi.wwwtidea.com
Here's a simple Java program to print an array:
public class PrintArrayExample { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } }
In this program, we have an integer array arr
with some values. We use a for
loop to iterate over the array and print each element to the console. We use the length
property of the array to determine the number of iterations needed to print all elements.
Output:
1 2 3 4 5
This program prints all the elements of the array to the console. If you want to print only a subset of the array, you can modify the loop accordingly. For example, you can use a range of indices to print a subarray.