Saturday, August 7, 2021

How to print array in java

To print any array in java program we need to use java.util.Arrays.toString(obj) method, This is availabe since java 5.

example :

public class PrintArray {

public static void main(String args[]) {

int[] arr = {23,4,5,6,71,5,4};

System.out.println(Arrays.toString(arr));

}

}

In case if  an array contains another array then in this case we need String representation of deep content like this:

example : Arrays.deepToString(object).

public class PrintArray {

public static void main(String args[]) {

String[] arr1= {"one", "two", "three"};

String[] arr2= {"four", "five", "six"};

String [][] arr = {arr1 , arr2};

System.out.println(Arrays.deepToString(arr));

}

}

Labels: , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home