Java Array to String toString(Object[] array)

Here you can find the source of toString(Object[] array)

Description

Create a string representation of the given array

License

Open Source License

Parameter

Parameter Description
array array to print

Return

array as a String

Declaration

public static String toString(Object[] array) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**//from www  .j  a va  2s  .co  m
     * Convert the list of objects to a list of strings.
     *
     * @param l List of objects
     * @return List of strings.
     */
    public static List toString(List l) {
        List stringList = new ArrayList();
        for (int i = 0; i < l.size(); i++) {
            stringList.add(l.get(i).toString());
        }
        return stringList;
    }

    /**
     * Create a string representation of the given array
     *
     * @param array array to print
     * @return array as a String
     */
    public static String toString(Object[] array) {
        StringBuffer buf = new StringBuffer();
        buf.append(": ");
        for (int i = 0; i < array.length; i++) {
            buf.append("[");
            buf.append(i);
            buf.append("]: ");
            buf.append((array[i] == null) ? "null" : array[i]);
            buf.append(" ");
        }
        return buf.toString();
    }

    /**
     * A utility method to an append to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null the we append to the StringBuffer the results of s1.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 object to append
     * @return StringBuffer with appended object
     */
    public static StringBuffer append(StringBuffer sb, Object s1) {
        if (sb == null) {
            sb = new StringBuffer();
        }
        sb.append((s1 == null) ? "null" : s1.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2) {
        sb = append(sb, s1);
        sb.append((s2 == null) ? "null" : s2.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3) {
        sb = append(sb, s1, s2);
        sb.append((s3 == null) ? "null" : s3.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3, Object s4) {
        sb = append(sb, s1, s2, s3);
        sb.append((s4 == null) ? "null" : s4.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @param s5 fifth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3, Object s4, Object s5) {
        sb = append(sb, s1, s2, s3, s4);
        sb.append((s5 == null) ? "null" : s5.toString());
        return sb;
    }
}

Related

  1. toString(Object array[])
  2. toString(Object[] a)
  3. toString(Object[] array)
  4. toString(Object[] array)
  5. toString(Object[] array)
  6. toString(Object[] objects)
  7. toString(String arrayName, T[] targets)
  8. toString(String[] args, char color1, char color2)
  9. toString(String[] arguments)