Here you can find the source of toString(Object array)
Parameter | Description |
---|---|
array | an array |
Parameter | Description |
---|---|
IllegalArgumentException | if array is not actually an array |
public static String toString(Object array)
//package com.java2s; //License from project: Apache License import java.util.Arrays; public class Main { /**//w w w . j ava 2 s.com * Constructs a string representation for the given array that describes its contents (as * opposed to the default {@link Object#toString()} version that arrays inherit). This provides a * convenience above and beyond similar methods in {@link Arrays} because it accepts any array * type, including primitive array types. * * @param array an array * @return a string representation for the given array that describes its contents * @throws IllegalArgumentException if {@code array} is not actually an array * * @see Arrays#toString(Object[]) */ public static String toString(Object array) { Class<?> arrayType = array.getClass(); if (!arrayType.isArray()) { throw new IllegalArgumentException("specified object is not an array"); } Class<?> componentType = arrayType.getComponentType(); if (componentType.isPrimitive()) { if (componentType == boolean.class) { return Arrays.toString((boolean[]) array); } else if (componentType == byte.class) { return Arrays.toString((byte[]) array); } else if (componentType == char.class) { return Arrays.toString((char[]) array); } else if (componentType == short.class) { return Arrays.toString((short[]) array); } else if (componentType == int.class) { return Arrays.toString((int[]) array); } else if (componentType == long.class) { return Arrays.toString((long[]) array); } else if (componentType == float.class) { return Arrays.toString((float[]) array); } else if (componentType == double.class) { return Arrays.toString((double[]) array); } else { throw new AssertionError("Unrecognized primitive type: " + componentType); } } else { return Arrays.toString((Object[]) array); } } }