Convert array to String - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Convert array to String

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] array = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(toString(array));
    }/*from www.jav  a  2s  .com*/

    public static String toString(int[] array) {
        StringBuilder result = new StringBuilder("{");

        if (array != null && array.length > 0) {
            for (int i = 0; i < array.length - 1; i++) {
                result.append(array[i] + ", ");
            }

            result.append(array[array.length - 1]);
        }

        result.append("}");

        return result.toString();
    }

    public static String toString(double[] array) {
        StringBuilder result = new StringBuilder("{");

        if (array != null && array.length > 0) {
            for (int i = 0; i < array.length - 1; i++) {
                result.append(array[i] + ", ");
            }

            result.append(array[array.length - 1]);
        }

        result.append("}");

        return result.toString();
    }
}

Related Tutorials