Convert an object array to a string, suitable for use in toString methods of the *Stat classes. - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Convert an object array to a string, suitable for use in toString methods of the *Stat classes.

Demo Code

/*-//from  w  ww. j a  v a 2 s  . c o  m
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2001,2007 Oracle.  All rights reserved.
 *
 * $Id: DbUtil.java,v 12.5 2007/05/17 15:15:42 bostic Exp $
 */
//package com.java2s;

public class Main {
    /**
     *  Convert an object array to a string, suitable for use in
     *  toString methods of the *Stat classes.
     *
     * @return    Description of the Return Value
     */
    public static String objectArrayToString(Object[] arr, String name) {
        if (arr == null) {
            return "null";
        }

        StringBuffer sb = new StringBuffer();
        int len = arr.length;
        for (int i = 0; i < len; i++) {
            sb.append("\n    " + name + "[" + i + "]:\n");
            sb.append("    " + arr[i].toString());
        }
        return sb.toString();
    }
}

Related Tutorials