Java Print Array Elements | Ways | Simple examples

There are many ways to get print Array Elements. Using a Loop statement is the most program used. Maybe some of you only knew for-loop. But in this tutorial, you will learn 3 ways to Java Print Array Content with examples.

Java Print Array Elements, Ways with Simple examples

Ways to Java Print Array

There are three ways to print an array.

  • Print an Array using For loop
  • Standard library static method – Arrays.toString(array)
  • Stream and lambda to print the array.

Note: When you tried to Print Array element using the toString() method. You will get Output the className + @ + the hex of the hashCode of the array.

Java Print Array examples

Let’s start looking one by one each method to get print data from the array in java. This all examples are for a one-dimensional array.

1. For loop

This can be your interview question like – How to print string array in java using for loop?

In Java there are many loops like – for-loop, for-each, do-while and while loop. For this example, we will use a for-loop. You can use other ones in the same way as below print string array in java using for loop.

Output: Array
Tutorial
Example

Another example of int values or how to print int array.

Below an example using a for-each loop to iterate over the given array,

Output: 7
6
4

2. Standard library – Arrays.toString(array)

You need to import a java.util.Arrays package for that example. You don’t need to run in a loop for it, just one line code – Arrays.toString(array) required. But in the output, you can see the different, above example simple print the array element but using Arrays.toString(array) print as an array.

Output: [1, 2, 3, 4, 5]

3. Stream and lambda to print the array

Prior to Java 8 – you can use Arrays.toString(array) to print one-dimensional array and Arrays.deepToString(array) for multi-dimensional arrays.

Java 8 – Have a new way of Stream and lambda to print the array. See the below examples, how to use it.

Printing One dimensional Array:

Output: 1
2
5
AB
CD
EF

Printing Multi-dimensional Array

Output: 11
12
31
32
33
A
B
C
D

How to Print a Multi-dimenstional Array?

Print 2D array or Nested Array in java needed deepToString() method. See below example – print elements of array java. This function works for  3-dimensional arrays as well.

Output: [[1, 2], [3, 4], [5, 6, 7]]

double Array Example:

Output: [5.0, 2.0, 5.0, 1.0, 3.0 ]

int Array Example:

Output: [3, 7, 5, 1, 3 ]

Question: How to print array in java without loop?

Answer: You can use a Standard library static method – Arrays.toString(array) and Stream and lambda to print the array. This method not required the use of a loop statement.

Do comment if you have any doubts and suggestions or examples.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java Print Array Elements is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.