Java Array – How To Print Elements Of An Array In Java?

This Tutorial will Explain the Various Methods to Print Elements of an Array in Java. Methods Explained are – Arrays.toString, For Loop, For Each Loop, & DeepToString:

In our previous tutorial, we discussed the creation of Array Initialization. To begin with, we declare instantiate and initialize the array. Once we do that, we process the array elements. After this, we need to print the output which consists of array elements.

Print Java Array

Methods To Print An Array In Java

There are various methods to print the array elements. We can convert the array to a string and print that string. We can also use the loops to iterate through the array and print element one by one.

Let's explore the description of these methods.

#1) Arrays.toString

This is the method to print Java array elements without using a loop. The method ‘toString’ belong to Arrays class of ‘java.util’ package.

The method ‘toString’ converts the array (passed as an argument to it) to the string representation. You can then directly print the string representation of the array.

The program below implements the toString method to print the array.

import java.util.Arrays;
public class Main
{
	public static void main(String[] args) {
		//array of strings
		String[] str_array = {"one","two","three","four","five"};
		
		System.out.println("Array elements printed with toString:");
		//convert array to string with Arrays.toString		
		System.out.println(Arrays.toString(str_array));
	}
}

Output:

Arrays.toString - output

As you can see, its just a line of code that can print the entire array.

#2) Using For Loop

This is by far the most basic method to print or traverse through the array in all programming languages. Whenever a programmer is asked to print the array, the first thing that the programmer will do is start writing a loop. You can use for loop to access array elements.

Following is the program that demonstrates the usage of for loop in Java.

public class Main
{
	public static void main(String[] args) {
	   Integer[] myArray = {10,20,30,40,50};
	   System.out.println("The elements in the array are:");
	   for(int i =0; i<5;i++)			//iterate through every array element
	        System.out.print(myArray[i] + " ");	//print the array element
	}
}

Output:

Using for loop - output

The ‘for’ loop iterates through every element in Java and hence you should know when to stop. Therefore to access array elements using for loop, you should provide it with a counter that will tell how many times it has to iterate. The best counter is the size of the array (given by length property).

#3) Using For-Each Loop

You can also use the forEach loop of Java to access array elements. The implementation is similar to for loop in which we traverse through each array element but the syntax for forEach loop is a little different.

Let us implement a program.

public class Main
{
   public static void main(String[] args) {
	
	 Integer myArray[]={10,20,30,40,50};  
	 System.out.println("The elements in the array are:");  
	 for(Integer i:myArray) 		//for each loop to print array elements
		System.out.print(i + " ");  
    }
}

Output:

Using for-Each loop - output

When you use forEach, unlike for loop you don’t need a counter. This loop iterates through all the elements in the array until it reaches the end of the array and accesses each element. The ‘forEach’ loop is specifically used for accessing array elements.

We have visited almost all the methods that are used to print arrays. These methods work for one-dimensional arrays. When it comes to printing multi-dimensional arrays, as we have to print those arrays in a row by column fashion, we need to slightly modify our previous approaches.

We will discuss more on that in our tutorial on a two-dimensional array.

#4) DeepToString

‘deepToString’ that is used to print two-dimensional arrays is similar to the ‘toString’ method which we discussed earlier. This is because if you just use ‘toString’, as the structure is array inside the array for multidimensional arrays; it will just print the addresses of the elements.

Hence we use the ‘deepToString’ function of Arrays class to print the multi-dimensional array elements.

The following program will show the ‘deepToString’  method.

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

	   //2D array of 3x3 dimensions

       int[][] array_2d = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

       System.out.println("Two-dimensional Array is as follows:");

       System.out.println(Arrays.deepToString(array_2d));	   //convert 2d array to string and display

    }
}

Output:

DeepToString

We will discuss some more methods of printing multidimensional arrays in our tutorial on multidimensional arrays.

Frequently Asked Questions

Q #1) Explain the toString method.

Answer: ‘toString()’ method is used to convert any entity passed to it to a string representation. The entity can be a variable, an array, a list, etc.

Q #2) What is the Arrays.toString in Java?

Answer:‘toString ()’ method returns the string representation of the array that is passed to it as an argument. The elements of the array are enclosed in a square ([]) bracket when displayed using the ‘toString()’ method.

Q #3) Do Arrays have a toString method?

Answer: There is no direct ‘toString’ method that you can use on an array variable. But the class ‘Arrays’ from ‘java.util’ package has a ‘toString’ method that takes the array variable as an argument and converts it to a string representation.

Q #4) What is ‘fill’ in Java?

Answer: The fill () method is used to fill the specified value to each element of the array. This method is a part of the java.util.Arrays class.

Q #5) Which technique/loop in Java specifically works with Arrays?

Answer: The ‘for-each’ construct or enhanced for loop is a loop that specifically works with arrays. As you can see, it is used to iterate over each element in the array.

Conclusion

In this tutorial, we explained the methods that we can use to print arrays. Mostly we employ loops to traverse and print the array elements one by one. In most cases, we need to know when to stop while using loops.

ForEach construct of Java is specifically used to traverse the object collection including arrays. We have also seen the toString method of Arrays class that converts the array into a string representation and we can directly display the string.

This tutorial was for printing a one-dimensional array. We also discussed a method of printing multi-dimensional arrays. We will discuss the other methods or variations of existing methods when we take up the topic of multi-dimensional arrays in the latter part of this series.