Java Program to Print Elements of an Array
In Java, an array stores multiple values of the same type. To print every element, you can traverse the array with a while loop, a traditional for loop, or an enhanced for loop. If you only need a compact representation of the whole array, Java also provides Arrays.toString() for one-dimensional arrays.
This tutorial shows each approach with complete Java programs and explains when each method is useful. It also includes examples for printing a String[] array and printing values entered with Scanner.
Print Array Elements using While Loop
To traverse through elements of an array using while loop, initialize an index variable with zero before while loop, and increment it in the while loop. Prepare a while loop with condition that checks if the index is still within the bounds of the array. And for the body of while loop, print the element of array by accessing it using array variable name and index.
While Loop Algorithm for Printing Array Elements
Following would be the detailed steps to print elements of array.
- Start.
- Take array in nums.
- Initialize an variable for index and initialize it to zero.
- Check if index is less than length of the array nums. If the condition is false, go to step 7.
- Access the element nums[index] and print it.
- Increment index. Go to step 4.
- Stop.
Main.java
public class Main {
public static void main(String[] args) {
int[] nums = {25, 87, 69, 55};
int index = 0;
while (index < nums.length) {
int num = nums[index];
System.out.println(num);
index++;
}
}
}
Output
25
87
69
55
The condition index < nums.length keeps the index within the valid range from 0 through nums.length - 1. The loop stops before an invalid array index is accessed.
Print Array Elements using For Loop
We can use for loop to iterate over array elements and print them during each iteration.
The difference between while loop and for loop is that we write statements to initialize and update loop control variables in the for loop in a single line.
The algorithm we used for the above example using while loop, will still hold for this program of printing array elements using for loop.
In the following java program, we shall use for loop to iterate and print the element of given array.
Main.java
public class Main {
public static void main(String[] args) {
int[] nums = {25, 87, 69, 55};
for(int index = 0; index < nums.length; index++) {
int num = nums[index];
System.out.println(num);
}
}
}
Output
25
87
69
55
Use an index-based for loop when you need both the array element and its position. For example, nums[index] lets you use or display the current index together with the value.
Print Array Elements using Enhanced For Loop
Advanced For Loop in Java is an enhancement for the regular loop, in case of iterating over an iterable.
In the following program, we shall iterate over the array nums. During each iteration, we get the element to variable num.
Main.java
public class Main {
public static void main(String[] args) {
int[] nums = {25, 87, 69, 55};
for(int num: nums) {
System.out.println(num);
}
}
}
Output
25
87
69
55
The enhanced for loop is usually the simplest choice when you only need each value and do not need the element’s index. It reads each array element from beginning to end.
Print an Entire Java Array with Arrays.toString()
If you want to display a one-dimensional array on a single line, use Arrays.toString(). Printing the array variable directly with System.out.println(nums) does not print its elements in the same readable form.
Main.java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = {25, 87, 69, 55};
System.out.println(Arrays.toString(nums));
}
}
Output
[25, 87, 69, 55]
Arrays.toString() is convenient for quick display and debugging. Use a loop instead when you need custom formatting, one element per line, filtering, calculations, or access to each value while traversing the array.
Print a String Array in Java
The same looping techniques work with arrays of objects such as String[]. In this example, an enhanced for loop prints each name on a separate line.
Main.java
public class Main {
public static void main(String[] args) {
String[] names = {"Asha", "Ravi", "Neha"};
for (String name : names) {
System.out.println(name);
}
}
}
Output
Asha
Ravi
Neha
Read and Print Array Elements using Scanner
When the array values come from user input, first create the array with the required size, read each element using Scanner, and then traverse the array to print the stored values.
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter array size: ");
int size = scanner.nextInt();
int[] nums = new int[size];
System.out.println("Enter " + size + " integers:");
for (int i = 0; i < nums.length; i++) {
nums[i] = scanner.nextInt();
}
System.out.println("Array elements:");
for (int num : nums) {
System.out.println(num);
}
scanner.close();
}
}
Sample input and output
Enter array size: 4
Enter 4 integers:
10
20
30
40
Array elements:
10
20
30
40
Choose the Right Way to Print Java Array Elements
- Use a while loop when the loop control is naturally managed outside the loop header.
- Use a traditional for loop when you need the array index.
- Use an enhanced for loop when you only need each element.
- Use Arrays.toString() when you want a compact, readable representation of a one-dimensional array.
- Use Scanner with a loop when the array elements must be read at runtime before they are printed.
Common Mistakes When Printing Java Arrays
- Do not use
index <= nums.lengthas the loop condition. The last valid index isnums.length - 1, so the correct condition isindex < nums.length. - Do not assume that printing an array variable directly prints all elements in a readable list. Use a loop or
Arrays.toString(). - Make sure the loop variable advances when using a
whileloop; otherwise the loop may never terminate. - Use an index-based loop rather than an enhanced
forloop when you need to know the position of each element.
Summary of Ways to Print Array Elements in Java
In this Java Tutorial, we have written Java programs to print array elements, using different looping statements available in Java.
TutorialKart.com