Java – How to print an Array

In this article, we will see how to print elements of an Array

Read Various ways to iterate Arrays in Java – 5 ways

1. Prints type@alphaNumericCharacter of an Array :

  • If we try to print an Array straight-forward, then it will simply print type@randomAlphaNumeric of an Array
  • To check, whether elements are present or not and what are its contents, for that we need to iterate an Array or use Arrays class’ method
package net.bench.resources.print.array;

public class PrintArrayDirectly {

	public static void main(String[] args) {

		// 1. Wrapper/Boxed Integer array
		Integer[] intArray = {1,2,3,4,5};

		// 1.1 print to console directly
		System.out.println("Integer[] array :- " + intArray);

		// 2. String array
		String[] stringArray = {
				"Power",
				"Motor",
				"Chemical",
				"Consumer",
				"Steel"
		};

		// 2.1 print to console directly
		System.out.println("String[] array :- " + stringArray);
	}
}

Output:

Integer[] array :- [Ljava.lang.Integer;@1db9742
String[] array :- [Ljava.lang.String;@106d69c

2. Print by iterating an Array using for-loop :

  • The easiest approach is to iterate an Array using traditional for-loop introduced in Java 1.0 version
  • This will print each elements of an Array 1-by-1 to console using index-position
package net.bench.resources.print.array;

public class PrintArrayUsingForLoop {

	public static void main(String[] args) {

		// 1. Wrapper/Boxed Integer array
		Integer[] intArray = {1,2,3,4,5};

		// 1.1 print using regular for-loop
		System.out.println("Integer[] array :- ");
		for(int index = 0; index < intArray.length; index++) {
			System.out.print(intArray[index] + " ");
		}

		// 2. String array
		String[] stringArray = {
				"Power",
				"Motor",
				"Chemical",
				"Consumer",
				"Steel"
		};

		// 2.1 print using regular for-loop
		System.out.println("\n\n\nString[] array :- \n");
		for(int index = 0; index < stringArray.length; index++) {
			System.out.println(stringArray[index]);
		}
	}
}

Output:

Integer[] array :- 
1 2 3 4 5 


String[] array :- 

Power
Motor
Chemical
Consumer
Steel

3. Print by iterating an Array using enhanced for-each loop :

  • This the next approach where we can iterate an Array using enhanced for-each loop introduced in Java 1.5 version
  • This will print each elements of an Array 1-by-1 to console
  • Note: this doesn’t provide explicit index-position unlike traditional for-loop
package net.bench.resources.print.array;

public class PrintArrayUsingEnhancedForLoop {

	public static void main(String[] args) {

		// 1. primitive int[] array
		int[] intArray = {1,2,3,4,5};

		// 1.1 print using enhanced for-loop
		System.out.println("primitive int[] array :- ");

		for(int value : intArray) {
			System.out.print(value + " ");
		}

		// 2. String array
		String[] stringArray = {
				"Power",
				"Motor",
				"Chemical",
				"Consumer",
				"Steel"
		};

		// 2.1 print using enhanced for-loop
		System.out.println("\n\nString[] array :- ");

		for(String str : stringArray) {
			System.out.println(str);
		}
	}
}

Output:

primitive int[] array :- 
1 2 3 4 5 

String[] array :- 
Power
Motor
Chemical
Consumer
Steel

4. Print by iterating an Array using Stream.forEach() :

  • Latest addition is to use Stream’s forEach() method to iterate an Array which is introduced in Java 1.8 version
  • We can pass either Lambda Expression or Method reference to forEach() method to print values to console
package net.bench.resources.print.array;

import java.util.Arrays;
import java.util.stream.Stream;

public class PrintArrayUsingStreamForEachLoop {

	public static void main(String[] args) {

		// 1. Wrapper/Boxed Integer array
		Integer[] intArray = {1,2,3,4,5};

		// 1.1 print using Java 8 Stream forEach loop
		System.out.println("Integer[] array :- ");

		// 1.2 Lambda expression
		Arrays
		.stream(intArray)
		.forEach(num -> System.out.println(num));


		// 2. String array
		String[] stringArray = {
				"Power",
				"Motor",
				"Chemical",
				"Consumer",
				"Steel"
		};

		// 2.1 print using Java 8 Stream forEach loop
		System.out.println("\n\nString[] array :- \n");

		// 2.2 Method reference
		Stream
		.of(stringArray)
		.forEach(System.out::println);
	}
}

Output:

Integer[] array :- 
1
2
3
4
5


String[] array :- 

Power
Motor
Chemical
Consumer
Steel

5. Print by iterating an Array using Iterator interface :

  • We can also get iterator after converting Array to a List
  • Then using while-loop (i.e.; hasNext() and next() methods) we can iterate/print Array elements to console
package net.bench.resources.print.array;

import java.util.Arrays;
import java.util.Iterator;

public class PrintArrayUsingIterator {

	public static void main(String[] args) {

		// 1. Wrapper/Boxed Integer array
		Integer[] intArray = {1,2,3,4,5};

		// 1.1 get iterator after converting to list-view
		Iterator<Integer> iterator = Arrays
				.asList(intArray)
				.iterator();

		System.out.println("Integer[] array :- ");

		// 1.2 iterate using while loop
		while(iterator.hasNext()) {

			// 1.3 print to console
			System.out.println(iterator.next());  
		}  

		// 2. String array
		String[] stringArray = {
				"Power",
				"Motor",
				"Chemical",
				"Consumer",
				"Steel"
		};

		// 2.1 get iterator after converting to list-view
		Iterator<String> strIterator = Arrays
				.asList(stringArray)
				.iterator();

		System.out.println("\n\nString[] array :- \n");

		// 2.2 iterate using while loop
		while(strIterator.hasNext()) {

			// 2.3 print to console
			System.out.println(strIterator.next());  
		} 
	}
}

Output:

Integer[] array :- 
1
2
3
4
5


String[] array :- 

Power
Motor
Chemical
Consumer
Steel

6. Print an Array after converting to List :

  • First, convert Array to List using Arrays.asList(arr)
  • Print list values directly to console
  • Note: actually asList() method provide list-view of an Array
package net.bench.resources.print.array;

import java.util.Arrays;
import java.util.List;

public class PrintArrayAfterConvertingToList {

	public static void main(String[] args) {

		// 1. Wrapper/Boxed Integer array
		Integer[] intArray = {1,2,3,4,5};

		// 1.1 convert to list-view
		List<Integer> integerList = Arrays.asList(intArray);

		// 1.2 print to console
		System.out.println("Integer[] array :- " + integerList);


		// 2. String array
		String[] stringArray = {
				"Power",
				"Motor",
				"Chemical",
				"Consumer",
				"Steel"
		};

		// 2.1 convert to list-view
		List<String> stringList = Arrays.asList(stringArray);

		// 2.2 print to console
		System.out.println("\nString[] array :- " + stringList);
	}
}

Output:

Integer[] array :- [1, 2, 3, 4, 5]

String[] array :- [Power, Motor, Chemical, Consumer, Steel]

7. Print an Array using Arrays.toString() method :

  • This static method helps to convert Array to String
  • We can print values of an Array to console
package net.bench.resources.print.array;

import java.util.Arrays;

public class PrintArrayUsingToString {

	public static void main(String[] args) {

		// 1. Wrapper/Boxed Integer array
		Integer[] intArray = {1,2,3,4,5};

		// 1.1 print to console
		System.out.println("Integer[] array :- " 
				+ Arrays.toString(intArray));


		// 2. String array
		String[] stringArray = {
				"Power",
				"Motor",
				"Chemical",
				"Consumer",
				"Steel"
		};

		// 2.2 print to console
		System.out.println("\nString[] array :- " 
				+ Arrays.toString(stringArray));
	}
}

Output:

Integer[] array :- [1, 2, 3, 4, 5]

String[] array :- [Power, Motor, Chemical, Consumer, Steel]

8. Print an Array using Arrays.deepToString() method :

  • This is another static method helps to convert Array to String specifically for 2-dimentional Array
  • We can print values of an 2-D Array to console
package net.bench.resources.print.array;

import java.util.Arrays;

public class Print2dArrayUsingDeepToString {

	public static void main(String[] args) {

		// 1. 2-d double array
		double[][] dbl2dArray = {
				{10.7, 20.8}, 
				{30.9, 40.6}, 
				{50.5, 60.3}
		};

		// 1.1 print to console
		System.out.println("double[][] array :- " 
				+ Arrays.deepToString(dbl2dArray));


		// 2. 2-d String array
		String[][] str2dArray = {
				{"Sachin", "Sourav", "Dravid"}, 
				{"Yuvraj", "Kaif"}, 
				{"Dhoni", "Raina"},
				{"Kohli", "Jadeja", "Rohit", "Bumrah"}
		};

		// 2.1 print to console
		System.out.println("\nString[][] array :- " 
				+ Arrays.deepToString(str2dArray));
	}
}

Output:

double[][] array :- [
[10.7, 20.8], 
[30.9, 40.6], 
[50.5, 60.3]
]

String[][] array :- [
[Sachin, Sourav, Dravid], 
[Yuvraj, Kaif], 
[Dhoni, Raina], 
[Kohli, Jadeja, Rohit, Bumrah]
]

References:

Happy Coding !!
Happy Learning !!

Java 8 - Stream reduce() method with examples
Java 8 - Array to Stream conversion