Java Array to String arrayToString(Object obj)

Here you can find the source of arrayToString(Object obj)

Description

array To String

License

Apache License

Declaration

public static String arrayToString(Object obj) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {

    public static String arrayToString(Object obj) {
        if (null == obj) {
            return null;
        }/*ww w. ja v  a2  s . co m*/
        if (isArray(obj)) {
            try {
                return Arrays.deepToString((Object[]) obj);
            } catch (Exception e) {
                final String className = obj.getClass().getComponentType().getName();
                switch (className) {
                case "long":
                    return Arrays.toString((long[]) obj);
                case "int":
                    return Arrays.toString((int[]) obj);
                case "short":
                    return Arrays.toString((short[]) obj);
                case "char":
                    return Arrays.toString((char[]) obj);
                case "byte":
                    return Arrays.toString((byte[]) obj);
                case "boolean":
                    return Arrays.toString((boolean[]) obj);
                case "float":
                    return Arrays.toString((float[]) obj);
                case "double":
                    return Arrays.toString((double[]) obj);
                default:
                    throw new RuntimeException(e);
                }
            }
        }
        return obj.toString();
    }

    private static boolean isArray(Object obj) {
        if (null == obj) {
            // throw new NullPointerException("Object check for isArray is null");
            return false;
        }
        return obj.getClass().isArray();
    }
}

Related

  1. arrayToString(int[][] array)
  2. arrayToString(Object arbitraryArray)
  3. arrayToString(Object array)
  4. arrayToString(Object array)
  5. arrayToString(Object array)
  6. arrayToString(Object obj[])
  7. arrayToString(Object... keys)
  8. arrayToString(Object... objects)
  9. arrayToString(Object[] a)