Print an Object array to a String. - Java java.lang

Java examples for java.lang:String Array

Description

Print an Object array to a String.

Demo Code

/*/*from  w w  w . j av a2  s  .  c  o  m*/
  You may freely copy, distribute, modify and use this class as long
  as the original author attribution remains intact.  See message
  below.

  Copyright (C) 2003 Christian Pesch. All Rights Reserved.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object[] array = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        System.out.println(printArrayToString(array));
    }

    /**
     * Print an Object ArrayHelper to a String.
     */
    public static String printArrayToString(Object[] array) {
        if (array == null)
            return "null";

        StringBuffer buffer = new StringBuffer();
        buffer.append('{');
        for (int i = 0; i < array.length; i++) {
            if (i > 0)
                buffer.append(',');
            buffer.append(array[i]);
        }
        buffer.append('}');
        return buffer.toString();
    }
}

Related Tutorials