Java Array to String arrayToString(Object array)

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

Description

Returns a string representation of the given array.

License

Apache License

Parameter

Parameter Description
array The array to create a string representation for.

Exception

Parameter Description
IllegalArgumentException If the given object is no array.

Return

The string representation of the array.

Declaration

public static String arrayToString(Object array) 

Method Source Code

//package com.java2s;
/*/*from w w w  .  j a  v a  2 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 {
    /**
     * Returns a string representation of the given array. This method takes an Object
     * to allow also all types of primitive type arrays.
     * 
     * @param array The array to create a string representation for.
     * @return The string representation of the array.
     * @throws IllegalArgumentException If the given object is no array.
     */
    public static String arrayToString(Object array) {
        if (array == null) {
            throw new NullPointerException();
        }

        if (array instanceof int[]) {
            return Arrays.toString((int[]) array);
        }
        if (array instanceof long[]) {
            return Arrays.toString((long[]) array);
        }
        if (array instanceof Object[]) {
            return Arrays.toString((Object[]) array);
        }
        if (array instanceof byte[]) {
            return Arrays.toString((byte[]) array);
        }
        if (array instanceof double[]) {
            return Arrays.toString((double[]) array);
        }
        if (array instanceof float[]) {
            return Arrays.toString((float[]) array);
        }
        if (array instanceof boolean[]) {
            return Arrays.toString((boolean[]) array);
        }
        if (array instanceof char[]) {
            return Arrays.toString((char[]) array);
        }
        if (array instanceof short[]) {
            return Arrays.toString((short[]) array);
        }

        if (array.getClass().isArray()) {
            return "<unknown array type>";
        } else {
            throw new IllegalArgumentException(
                    "The given argument is no array.");
        }
    }
}

Related

  1. arrayToString(int[] s)
  2. arrayToString(int[] subset)
  3. arrayToString(int[][] array)
  4. arrayToString(Object arbitraryArray)
  5. arrayToString(Object array)
  6. arrayToString(Object array)
  7. arrayToString(Object obj)
  8. arrayToString(Object obj[])
  9. arrayToString(Object... keys)