Here are several ways to print an array in Java, along with explanations and examples. Each method is useful in different scenarios.
1. Using a for
Loop
The most common way is to iterate through the array using a for
loop and print each element.
package com.javacodepoint.array;
public class PrintArrayUsingForLoop {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("Array elements using a for loop:");
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
}
}
OUTPUT:
Array elements using a for loop:
10 20 30 40 50
2. Using a for-each
Loop
A cleaner and more readable alternative is the enhanced for
loop (also called a for-each
loop).
package com.javacodepoint.array;
public class PrintArrayUsingForEach {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("Array elements using a for-each loop:");
for (int num : numbers) {
System.out.print(num + " ");
}
}
}
OUTPUT:
Array elements using a for-each loop:
10 20 30 40 50
3. Using Arrays.toString()
The Arrays.toString()
method from the java.util
package converts the array to a string in a single step.
package com.javacodepoint.array;
import java.util.Arrays;
public class PrintArrayUsingToString {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("Array elements using Arrays.toString():");
System.out.println(Arrays.toString(numbers));
}
}
OUTPUT:
Array elements using Arrays.toString():
[10, 20, 30, 40, 50]
4. Using Arrays.stream()
For Java 8 and later, you can use Streams to print the array elements.
package com.javacodepoint.array;
import java.util.Arrays;
public class PrintArrayUsingStream {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("Array elements using Streams:");
Arrays.stream(numbers).forEach(num -> System.out.print(num + " "));
}
}
OUTPUT:
Array elements using Streams:
10 20 30 40 50
5. Using String.join()
for String Arrays
If the array contains strings, you can use String.join()
to concatenate the elements into a single string.
package com.javacodepoint.array;
public class PrintStringArrayUsingJoin {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry"};
System.out.println("Array elements using String.join():");
System.out.println(String.join(", ", fruits));
}
}
OUTPUT:
Array elements using String.join():
Apple, Banana, Cherry
6. Using Collections
(For Lists)
If your array is converted to a List
, you can use the toString()
method of List
.
package com.javacodepoint.array;
import java.util.Arrays;
import java.util.List;
public class PrintArrayUsingList {
public static void main(String[] args) {
Integer[] numbers = {10, 20, 30, 40, 50};
List<Integer> numberList = Arrays.asList(numbers);
System.out.println("Array elements using List:");
System.out.println(numberList);
}
}
OUTPUT:
Array elements using List:
[10, 20, 30, 40, 50]
7. Using StringBuilder
for Custom Formatting
You can use a StringBuilder
to format the output as per your requirements.
package com.javacodepoint.array;
public class PrintArrayUsingStringBuilder {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
StringBuilder sb = new StringBuilder("Array elements: ");
for (int num : numbers) {
sb.append(num).append(", ");
}
// Remove the last comma and space
if (sb.length() > 0) {
sb.setLength(sb.length() - 2);
}
System.out.println(sb.toString());
}
}
OUTPUT:
Array elements: 10, 20, 30, 40, 50
8. Using Arrays.deepToString()
(For Multi-Dimensional Arrays)
For multi-dimensional arrays, Arrays.deepToString()
provides a structured output.
package com.javacodepoint.array;
import java.util.Arrays;
public class PrintMultiDimensionalArray {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("Multi-dimensional array:");
System.out.println(Arrays.deepToString(matrix));
}
}
OUTPUT:
Multi-dimensional array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]