How to Print an Array in Java?

How to Print an Array in Java?

While working with Java, we often need to display the elements of an array for debugging or output purposes. By printing the elements of an array, we can validate data, ensure the calculations are correct, and debug the errors. Java provides multiple methods to print an array.

In this blog, we will learn how we can print an array in Java using multiple methods.

Table of Contents:

Methods to Print an Array in Java

An array in Java is a data structure that is used to store multiple values of the same data type in a single variable name. Let’s explore all the different methods to print an array in Java:

Note: We have discussed multiple methods to print an array in Java, but the simplest and widely used method is for loop.

Method 1: Using for Loop to Print an Array in Java

To print an array in Java, we use a for loop. This is the simplest method for printing the elements of an array.

Example:

Java

Output:

Using for Loop to Print an Array in Java

Method 2: Using while Loop to Print an Array in Java

A while loop can also be used to print an array in Java. The while loop takes a condition as its argument, and as long as the condition is true, it continues executing the code written inside the loop.

Example:

Java

Output:

Using while Loop to Print an Array in Java

Method 3: Using for-each Loop to Print an Array in Java

The for-each loop is used to iterate through an array with a more concise syntax. The loop automatically iterates through all elements in the array, and we don’t need to maintain the index of the array.

Example:

Java

Output:

Using for-each Loop to Print an Array in Java

Method 4: Using Arrays.toString() Method to Print an Array in Java

Arrays.toString() is a built-in method in the java.util.Arrays class that converts an array into a readable string. It automatically formats the array, separating elements with commas and enclosing them in square brackets.

Example:

Java

Output:

Using Arrays.toString() Method to Print an Array in Java

Method 5: Using Arrays.deepToString() Method to Print an Array in Java

Arrays.deepToString() is used for printing the multidimensional array. It works similarly to Arrays.toString(), but it recursively converts the elements of nested arrays into a readable string.

Example:

Java

Code:

Using Arrays.deepToString() Method to Print an Array in Java

Method 6: Using Arrays.asList() Method to Print an Array in Java

Arrays.asList() converts an array into a fixed-size list. While it allows you to use List methods like toString(), it does not support resizing (e.g., adding or removing elements).

Example:

Java

Output:

image 129

Method 7: Using Java Iterator Interface to Print an Array in Java

The Iterator interface in Java is a part of the Java Collections Framework that allows you to traverse the elements of a collection (such as a List) one by one.

Example:

Java

Output:

Using Arrays.asList() Method to Print an Array in Java

Method 8: Using Java Stream API to Print an Array in Java

The Java Stream API offers a modern way to work with collections and arrays. By using Streams with forEach(), you can easily print the elements of an array.

1. Java stream() Method

The stream() method is part of the Java Stream API. It is used to convert a collection or array into a stream for processing data in a functional style.

Syntax:

Arrays.stream(array)

2. Java forEach() Method

The forEach() method is used to iterate over elements of a collection or a stream, and in each iteration, we can print the elements of the stream.

Syntax:

collection.forEach(action);
stream.forEach(action);

 Where,

  • A collection is any List, Set, or other Collection.
  • A stream is a stream of data.
  • Action is a lambda expression or method reference that specifies what to do with each element.

Example:

Java

Output:

Using Java Stream API to Print an Array in Java

Conclusion

So far in this article, we have learned different methods to print an array in Java. Among various methods, the most widely used method is using a for loop. You can use any one of the above methods to print the elements of the array. If you want to become a Java expert, you may refer to our Java Course.

FAQs

Q1. Can I modify the elements of an array while using the for-each loop?

No, you cannot modify elements of a primitive array(e.g., int[], boolean[]), etc.) using a for-each loop because it works with a copy of each element (pass-by-value). However, for an array of objects, you can modify their properties since the references still point to the same objects in memory.

Q2. Is it possible to print a multidimensional array using Arrays.toString()?

No, Arrays.toString() only works with one-dimensional arrays. We have to use Arrays.deepToString() for multidimensional arrays.

Q3. Can I use the Stream API with arrays of primitive types?

Yes, Arrays.stream() supports arrays of primitive types like int[].

Q4. How to print an array in Java without a loop?

You can print an array in Java without using a loop by using built-in methods from the Arrays class Arrays.toString().

Q5. How to get the length of an array?

You can get the length of an array in Java using the array.length property.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner