Java Array to String arrayToString(Object arbitraryArray)

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

Description

Converts an array (either Object[] or of a primitive type) to a String containing it's elements in square brackets.

License

Open Source License

Parameter

Parameter Description
arbitraryArray The array to convert to a String.

Exception

Parameter Description
IllegalArgumentException If arbitraryArray if not an array.

Return

A String representation of the array arbitraryArray .

Declaration

public static String arrayToString(Object arbitraryArray) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * This file is part of jasima, v1.3, the Java simulator for manufacturing and 
 * logistics./*from w  w w .  j  a v  a 2s .  co  m*/
 *  
 * Copyright (c) 2015       jasima solutions UG
 * Copyright (c) 2010-2015 Torsten Hildebrandt and jasima contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.util.Arrays;

public class Main {
    /**
     * Converts an array (either Object[] or of a primitive type) to a String
     * containing it's elements in square brackets.
     * 
     * @param arbitraryArray
     *            The array to convert to a String.
     * @return A String representation of the array {@code arbitraryArray}.
     * @throws IllegalArgumentException
     *             If {@code arbitraryArray} if not an array.
     */
    public static String arrayToString(Object arbitraryArray) throws IllegalArgumentException {
        Class<?> compType = arbitraryArray.getClass().getComponentType();
        if (compType == null)
            throw new IllegalArgumentException();

        if (compType.isPrimitive()) {
            if (compType == Integer.TYPE)
                return Arrays.toString((int[]) arbitraryArray);
            else if (compType == Long.TYPE)
                return Arrays.toString((long[]) arbitraryArray);
            else if (compType == Short.TYPE)
                return Arrays.toString((short[]) arbitraryArray);
            else if (compType == Byte.TYPE)
                return Arrays.toString((byte[]) arbitraryArray);
            else if (compType == Boolean.TYPE)
                return Arrays.toString((boolean[]) arbitraryArray);
            else if (compType == Double.TYPE)
                return Arrays.toString((double[]) arbitraryArray);
            else if (compType == Float.TYPE)
                return Arrays.toString((float[]) arbitraryArray);
            else if (compType == Character.TYPE)
                return Arrays.toString((char[]) arbitraryArray);
            else
                throw new AssertionError();
        } else
            return Arrays.deepToString((Object[]) arbitraryArray);
    }
}

Related

  1. arrayToString(int[] ary)
  2. arrayToString(int[] c, String separator)
  3. arrayToString(int[] s)
  4. arrayToString(int[] subset)
  5. arrayToString(int[][] array)
  6. arrayToString(Object array)
  7. arrayToString(Object array)
  8. arrayToString(Object array)
  9. arrayToString(Object obj)