Close

Java Collections - Arrays.toString() Examples

Code Snippets Java Collections Java Java API 


Class:

java.util.Arrays

java.lang.Objectjava.lang.Objectjava.util.Arraysjava.util.ArraysLogicBig

Methods:

These methods returns a string representation of the contents of the specified array. These methods do not recursively go into nested elements

public static String toString(long[] a)
public static String toString(int[] a)
public static String toString(short[] a)
public static String toString(char[] a)
public static String toString(byte[] a)
public static String toString(boolean[] a)
public static String toString(float[] a)
public static String toString(double[] a)
public static String toString(Object[] a)

Examples


package com.logicbig.example.arrays;

import java.util.Arrays;

public class ToStringExample {

public static void main(String... args) {
Object[] arr1 = {3, 5, 6, 7};

String s1 = arr1.toString();
System.out.println(s1);

String s2 = Arrays.toString(arr1);
System.out.println(s2);
}
}

Output

[Ljava.lang.Object;@606a17d
[3, 5, 6, 7]




For nested array, useArrays#deepToString()

package com.logicbig.example.arrays;

import java.util.Arrays;

public class ToStringExample2 {

public static void main(String... args) {
Object[] arr1 = {3, 5, new Object[]{6, 7, new int[]{9, 11}}};

String s1 = arr1.toString();
System.out.println(s1);

String s2 = Arrays.toString(arr1);
System.out.println(s2);

String s3 = Arrays.deepToString(arr1);
System.out.println(s3);
}
}

Output

[Ljava.lang.Object;@16884b5f
[3, 5, [Ljava.lang.Object;@5a97eed0]
[3, 5, [6, 7, [9, 11]]]




See Also