Join array of primitive type to string - Android java.lang

Android examples for java.lang:String Join

Description

Join array of primitive type to string

Demo Code

import java.util.List;
import java.util.Map;

public class Main{

    public static <T> String Join(List<T> list) {
        return Join(list, ",");
    }//  w  w w. j ava 2  s  . c o  m
    public static <T> String Join(List<T> list, String separator) {
        StringBuilder builder = new StringBuilder();

        int i = 0;
        for (T e : list) {
            builder.append(e);
            if (++i < list.size())
                builder.append(separator);
        }
        return builder.toString();
    }
    public static <T> String Join(T[] array) {
        return Join(array, ",");
    }
    public static <T> String Join(T[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            builder.append(array[i]);
            if (i < array.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }
    public static String Join(boolean[] array) {
        return Join(array, ",");
    }
    public static String Join(boolean[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            builder.append(array[i]);
            if (i < array.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }
    public static String Join(byte[] array) {
        return Join(array, ",");
    }
    public static String Join(byte[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            builder.append(array[i]);
            if (i < array.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }
    public static String Join(short[] array) {
        return Join(array, ",");
    }
    public static String Join(short[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            builder.append(array[i]);
            if (i < array.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }
    public static String Join(char[] array) {
        return Join(array, ",");
    }
    public static String Join(char[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            builder.append(array[i]);
            if (i < array.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }
    public static String Join(int[] array) {
        return Join(array, ",");
    }
    public static String Join(int[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            builder.append(array[i]);
            if (i < array.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }
    public static String Join(long[] array) {
        return Join(array, ",");
    }
    public static String Join(long[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            builder.append(array[i]);
            if (i < array.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }
    public static String Join(float[] array) {
        return Join(array, ",");
    }
    public static String Join(float[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            builder.append(array[i]);
            if (i < array.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }
    public static String Join(double[] array) {
        return Join(array, ",");
    }
    public static String Join(double[] array, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            builder.append(array[i]);
            if (i < array.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }

}

Related Tutorials