Java Array to String arrayToString(Object array)

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

Description

array To String

License

Apache License

Declaration

public static String arrayToString(Object array) 

Method Source Code

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

public class Main {
    public static String arrayToString(Object array) {
        if (array instanceof byte[])
            return arrayToString((byte[]) array);
        if (array instanceof short[])
            return arrayToString((short[]) array);
        if (array instanceof int[])
            return arrayToString((int[]) array);
        if (array instanceof long[])
            return arrayToString((long[]) array);
        if (array instanceof float[])
            return arrayToString((float[]) array);
        if (array instanceof double[])
            return arrayToString((double[]) array);
        if (array instanceof boolean[])
            return arrayToString((boolean[]) array);
        if (array instanceof char[])
            return arrayToString((char[]) array);
        return arrayToString((Object[]) array);
    }/*  w  ww . j a  v  a2  s  .  c  o  m*/

    private static String arrayToString(Object[] o) {
        StringBuilder sb = new StringBuilder();
        String comma = ", ";
        String empty = "";
        sb.append("[");
        for (int i = 0; i < o.length; i++)
            sb.append(i > 0 ? comma : empty).append(String.valueOf(o[i]));
        sb.append("]");
        return sb.toString();
    }

    private static String arrayToString(byte[] o) {
        StringBuilder sb = new StringBuilder();
        String comma = ", ";
        String empty = "";
        sb.append("[");
        for (int i = 0; i < o.length; i++)
            sb.append(i > 0 ? comma : empty).append(String.valueOf(o[i]));
        sb.append("]");
        return sb.toString();
    }

    private static String arrayToString(short[] o) {
        StringBuilder sb = new StringBuilder();
        String comma = ", ";
        String empty = "";
        sb.append("[");
        for (int i = 0; i < o.length; i++)
            sb.append(i > 0 ? comma : empty).append(String.valueOf(o[i]));
        sb.append("]");
        return sb.toString();
    }

    private static String arrayToString(int[] o) {
        StringBuilder sb = new StringBuilder();
        String comma = ", ";
        String empty = "";
        sb.append("[");
        for (int i = 0; i < o.length; i++)
            sb.append(i > 0 ? comma : empty).append(String.valueOf(o[i]));
        sb.append("]");
        return sb.toString();
    }

    private static String arrayToString(long[] o) {
        StringBuilder sb = new StringBuilder();
        String comma = ", ";
        String empty = "";
        sb.append("[");
        for (int i = 0; i < o.length; i++)
            sb.append(i > 0 ? comma : empty).append(String.valueOf(o[i]));
        sb.append("]");
        return sb.toString();
    }

    private static String arrayToString(float[] o) {
        StringBuilder sb = new StringBuilder();
        String comma = ", ";
        String empty = "";
        sb.append("[");
        for (int i = 0; i < o.length; i++)
            sb.append(i > 0 ? comma : empty).append(String.valueOf(o[i]));
        sb.append("]");
        return sb.toString();
    }

    private static String arrayToString(double[] o) {
        StringBuilder sb = new StringBuilder();
        String comma = ", ";
        String empty = "";
        sb.append("[");
        for (int i = 0; i < o.length; i++)
            sb.append(i > 0 ? comma : empty).append(String.valueOf(o[i]));
        sb.append("]");
        return sb.toString();
    }

    private static String arrayToString(boolean[] o) {
        StringBuilder sb = new StringBuilder();
        String comma = ", ";
        String empty = "";
        sb.append("[");
        for (int i = 0; i < o.length; i++)
            sb.append(i > 0 ? comma : empty).append(String.valueOf(o[i]));
        sb.append("]");
        return sb.toString();
    }

    private static String arrayToString(char[] o) {
        StringBuilder sb = new StringBuilder();
        String comma = ", ";
        String empty = "";
        sb.append("[");
        for (int i = 0; i < o.length; i++)
            sb.append(i > 0 ? comma : empty).append(String.valueOf(o[i]));
        sb.append("]");
        return sb.toString();
    }
}

Related

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