Simplest Method to Print Array in Java
Last Updated :
25 Jan, 2024
An array is a structure containing the element of a similar data type as one unit. In this article, we will learn the simplest method to print an array in Java.
Prerequisites
What is the Simplest Method to Print Array in Java?
Arrays.toString() method is used to print One-dimensional (1D) Array in Java. This can be invoked by importing “java.util.Arrays” class. To print an array using this method just pass the array into the arguments of the function.
Syntax:
Arrays.toString(printing_array)
Below is the implementation of printing Array with Arrays.toString() method:
Java
import java.util.Arrays;
public class PrintingArray {
public static void main(String[] args) {
int [] array = { 1 , 2 , 3 , 4 , 5 };
System.out.print( "Integer Array elements are: " );
System.out.println(Arrays.toString(array));
String[] stringArray = { "vivek" , "deeksha" , "chaudhary" };
System.out.print( "String Array elements are: " );
System.out.println(Arrays.toString(stringArray));
}
}
|
Output
Integer Array elements are: [1, 2, 3, 4, 5]
Double Array elements are: [1.13, 2.28, 3.33, 44.4, 54.5]
String Array elements are: [vivek, deeksha, chaudhary]
To know more the topic refer to Java Array article.
Share your thoughts in the comments
Please Login to comment...