print array to String - Android java.lang

Android examples for java.lang:Array Element

Description

print array to String

Demo Code


//package com.java2s;

public class Main {
    public static String print(int... array) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        if (array != null && array.length > 0)
            for (int i = 0; i < array.length; i++)
                sb.append(array[i]).append(
                        i == array.length - 1 ? "]" : ", ");
        else {//  w w w.  jav  a  2 s .c  om
            if (array != null && array.length == 0)
                sb.append("empty");

            sb.append("]");
        }
        return sb.toString();
    }

    public static String print(long... array) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        if (array != null && array.length > 0)
            for (int i = 0; i < array.length; i++)
                sb.append(array[i]).append(
                        i == array.length - 1 ? "]" : ", ");
        else
            sb.append("]");
        return sb.toString();
    }

    public static <M extends Object> String print(M... array) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        if (array != null && array.length > 0)
            for (int i = 0; i < array.length; i++)
                sb.append(array[i]).append(
                        i == array.length - 1 ? "]" : ", ");
        else
            sb.append("]");
        return sb.toString();
    }
}

Related Tutorials