Java Array Dimension Get arrayDimensions(Class arrayClass)

Here you can find the source of arrayDimensions(Class arrayClass)

Description

Get the dimension of an array

License

Open Source License

Parameter

Parameter Description
arrayClass The array class

Exception

Parameter Description
NullPointerException If arrayClass is null
IllegalArgumentException If arrayClass is not an array class

Return

Dimension, >= 0

Declaration

public static int arrayDimensions(Class<?> arrayClass) 

Method Source Code

//package com.java2s;
/*//from w ww .ja  v  a  2 s  . co m
 *
 *  The contents of this file are subject to the Terracotta Public 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://terracotta.org/legal/terracotta-public-license.
 *
 *  Software distributed under the License is distributed on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 *  the specific language governing rights and limitations under the License.
 *
 *  The Covered Software is Terracotta Core.
 *
 *  The Initial Developer of the Covered Software is
 *  Terracotta, Inc., a Software AG company
 *
 */

public class Main {
    /**
     * Get the dimension of an array
     * @param arrayClass The array class
     * @return Dimension, >= 0
     * @throws NullPointerException If arrayClass is null
     * @throws IllegalArgumentException If arrayClass is not an array class
     */
    public static int arrayDimensions(Class<?> arrayClass) {
        verifyIsArray(arrayClass); // guarantees c is non-null and an array class
        return arrayClass.getName().lastIndexOf("[") + 1;
    }

    private static void verifyIsArray(Class<?> arrayClass) {
        if (arrayClass == null) {
            throw new NullPointerException();
        }
        if (!arrayClass.isArray()) {
            throw new IllegalArgumentException(arrayClass + " is not an array type");
        }
    }
}

Related

  1. arrayDim(Class c)
  2. arrayDimension(String clsName)
  3. arrayDimensions(Class c)
  4. arraysDims(String[] arr)