Java Array to String arrayToString(int[] ary)

Here you can find the source of arrayToString(int[] ary)

Description

array To String

License

Apache License

Declaration

public static String arrayToString(int[] ary) 

Method Source Code

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

import java.util.Arrays;

public class Main {
    public static String arrayToString(int[] ary) {
        if (ary == null || ary.length == 0)
            return "";
        int m = ary.length - 1;

        StringBuilder sb = new StringBuilder();
        for (int i = 0;; i++) {
            sb.append(ary[i]);/* w w w  .  ja  v  a  2 s . c o m*/
            if (i == m)
                return sb.toString();
            sb.append(", ");
        }
    }

    static public double[][] append(double[][] a, double[][] b) {
        if (a == null)
            return b;
        if (b == null)
            return a;
        if (a.length == 0)
            return b;
        if (b.length == 0)
            return a;
        assert a[0].length == b[0].length;
        double[][] c = Arrays.copyOf(a, a.length + b.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    static public double[] append(double[] a, double[] b) {
        if (a == null)
            return b;
        if (b == null)
            return a;
        if (a.length == 0)
            return b;
        if (b.length == 0)
            return a;
        double[] c = Arrays.copyOf(a, a.length + b.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    static public String[] append(String[] a, String[] b) {
        if (a == null)
            return b;
        if (b == null)
            return a;
        if (a.length == 0)
            return b;
        if (b.length == 0)
            return a;
        String[] c = Arrays.copyOf(a, a.length + b.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static <T> T[] append(T[] a, T... b) {
        if (a == null)
            return b;
        T[] tmp = Arrays.copyOf(a, a.length + b.length);
        System.arraycopy(b, 0, tmp, a.length, b.length);
        return tmp;
    }

    public static String[] toString(long[] dom) {
        String[] result = new String[dom.length];
        for (int i = 0; i < dom.length; i++)
            result[i] = String.valueOf(dom[i]);
        return result;
    }

    public static String[] toString(int[] dom) {
        String[] result = new String[dom.length];
        for (int i = 0; i < dom.length; i++)
            result[i] = String.valueOf(dom[i]);
        return result;
    }

    public static String[] toString(Object[] ary) {
        String[] result = new String[ary.length];
        for (int i = 0; i < ary.length; i++) {
            Object o = ary[i];
            if (o != null && o.getClass().isArray()) {
                Class klazz = ary[i].getClass();
                result[i] = byte[].class.equals(klazz) ? Arrays
                        .toString((byte[]) o)
                        : short[].class.equals(klazz) ? Arrays
                                .toString((short[]) o)
                                : int[].class.equals(klazz) ? Arrays
                                        .toString((int[]) o)
                                        : long[].class.equals(klazz) ? Arrays
                                                .toString((long[]) o)
                                                : boolean[].class
                                                        .equals(klazz) ? Arrays
                                                        .toString((boolean[]) o)
                                                        : float[].class
                                                                .equals(klazz) ? Arrays
                                                                .toString((float[]) o)
                                                                : double[].class
                                                                        .equals(klazz) ? Arrays
                                                                        .toString((double[]) o)
                                                                        : Arrays.toString((Object[]) o);

            } else {
                result[i] = String.valueOf(o);
            }
        }
        return result;
    }
}

Related

  1. arrayToString(int[] array)
  2. arrayToString(int[] array)
  3. arrayToString(int[] array)
  4. arrayToString(int[] array)
  5. arrayToString(int[] array)
  6. arrayToString(int[] c, String separator)
  7. arrayToString(int[] s)
  8. arrayToString(int[] subset)
  9. arrayToString(int[][] array)