Java Array to String toString(Object array)

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

Description

Outputs an array as a String, treating null as an empty array.

License

Apache License

Parameter

Parameter Description
array the array to get a toString for, may be <code>null</code>

Return

a String representation of the array, '{}' if null array input

Declaration

public static String toString(Object array) 

Method Source Code

//package com.java2s;
/*/*from w w w.ja v  a2 s .  c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Arrays;

public class Main {
    /**
     * <p>
     * Outputs an array as a String, treating <code>null</code> as an empty
     * array.
     * </p>
     *
     * <p>
     * Multi-dimensional arrays are handled correctly, including
     * multi-dimensional primitive arrays.
     * </p>
     *
     * <p>
     * The format is that of Java source code, for example <code>{a,b}</code>.
     * </p>
     * 
     * @param array
     *            the array to get a toString for, may be <code>null</code>
     * @return a String representation of the array, '{}' if null array input
     */
    public static String toString(Object array) {
        return toString(array, "{}");
    }

    /**
     * <p>
     * Outputs an array as a String handling <code>null</code>s.
     * </p>
     *
     * <p>
     * Multi-dimensional arrays are handled correctly, including
     * multi-dimensional primitive arrays.
     * </p>
     *
     * <p>
     * The format is that of Java source code, for example <code>{a,b}</code>.
     * </p>
     * 
     * @param array
     *            the array to get a toString for, may be <code>null</code>
     * @param stringIfNull
     *            the String to return if the array is <code>null</code>
     * @return a String representation of the array
     */
    public static String toString(Object array, String stringIfNull) {
        if (array == null) {
            return stringIfNull;
        }
        return Arrays.deepToString((Object[]) array);
    }
}

Related

  1. toString(int[] v, char delim)
  2. toString(long[] dom)
  3. toString(Map arg)
  4. toString(Object array)
  5. toString(Object array)
  6. toString(Object array[])
  7. toString(Object[] a)
  8. toString(Object[] array)
  9. toString(Object[] array)