Builds a bracketed CSV list from the array - Java java.io

Java examples for java.io:File CSV

Description

Builds a bracketed CSV list from the array

Demo Code


//package com.java2s;
import java.lang.reflect.Array;

public class Main {
    public static void main(String[] argv) {
        Object array = "java2s.com";
        System.out.println(arrayToString(array));
    }//from w ww . ja v a2s  .  co m

    /**
     * Builds a bracketed CSV list from the array
     * @param array an array of Objects
     * @return string
     */
    public static String arrayToString(Object array) {

        int len = Array.getLength(array);
        int last = len - 1;
        StringBuffer sb = new StringBuffer(2 * (len + 1));

        sb.append('{');

        for (int i = 0; i < len; i++) {
            sb.append(Array.get(array, i));

            if (i != last) {
                sb.append(',');
            }
        }

        sb.append('}');

        return sb.toString();
    }
}

Related Tutorials