Java 二维数组的组件类型

huangapple go评论71阅读模式
英文:

Java Component Type of 2D Array

问题

我有一个由整数填充的二维数组 int[][] myArray

如果我执行 myArray.getClass().getComponentType(),它会返回 [I

为什么会这样呢?

编辑
我有另一个库可以检查我这个二维数组的组件类型。它只允许 Integer.TYPECharacter.TYPELong.TYPE 等等。
或者,这是否意味着通过这种检查,它不允许我使用二维数组?

英文:

I have a 2D array int[][] myArray populated with integers.

If I do myArray.getClass().getComponentType(), it returns me [I.

Why is that so?

Edit
I have another library to check the component type of my 2d array. It only allows Integer.TYPE, Character.TYPE, Long.TYPE and so on.
Or, does it mean that with this check, it does not allow me to use 2D array at all?

答案1

得分: 2

java.lang.Class类getComponentType()方法用于获取表示数组的组件类型的Class,如果此类表示一个数组的话。否则,它返回null。

getClass()是Object类的方法。此方法返回此对象的运行时类。

对于一维数组,例如:int arr[] = new int[5],当我们尝试获取类时,arr.getClass(),输出是

class [I

同样对于同一维数组,当我们尝试arr.getClass().getComponentType()时,输出是

int

二维数组可以称为数组的数组。

对于二维数组,例如:int arr[][] = new int[5][5],当我们尝试获取类时,arr.getClass(),输出是

class [[I

解释:class [ => 表示数组 [ => 表示数组 I => 表示整数

对于float arr[][],它将是

class [[F

当我们尝试arr.getClass().getComponentType()时,输出是

class [I

当我们再次尝试getComponetType(),即Arr.getClass().getComponentType().getComponentType()时,输出是

int

从上述结果可以清楚地看出,对于N维数组,我们必须重复获取组件类型,直到我们解开类类型。

英文:

The getComponentType() method of java.lang.Class class is used to get the Class representing the component type of an array, if this class represents one. Else it returns null.

The getClass() is the method of Object class. This method returns the runtime class of this object.

For 1D array , for example : int arr[] = new int[5], when we try to get class,arr.getClass() ,the output is

> class [I

Again for same 1D array, when we try arr.getClass().getComponentType(), the output is

> int

A 2D array can be called as an array of an array.

For 2D array , for example : int arr[][] = new int[5][5], when we try to get class,arr.getClass() ,the output is

> class [[I

Explanation :class [ => represents array [ => represents array I => represents Integer

for float arr[][], it would be

> class [[F

When we try arr.getClass().getComponentType(), the output is

> class [I

When we again try to getComponetType() , i.e. Arr.getClass().getComponentType().getComponentType(), we get output as

> int

From the above results, it is clearly seen that, for N-dimensional array we have to repeatedly Get Component Type until we unwrap the class Type

答案2

得分: 1

你可以使用递归方法:

private static Class<?> getComponentTypeOfMultidimensionalArray(Class<?> arrayType) {
    if (arrayType.isArray()) {
        return getComponentTypeOfMultidimensionalArray(arrayType.getComponentType());
    } else {
        return arrayType;
    }
}

用法:

System.out.println(getComponentTypeOfMultidimensionalArray(int[][].class)); // int
英文:

You can use a recursive method:

private static Class&lt;?&gt; getComponentTypeOfMultidimensionalArray(Class&lt;?&gt; arrayType) {
    if (arrayType.isArray()) {
        return getComponentTypeOfMultidimensionalArray(arrayType.getComponentType());
    } else {
        return arrayType;
    }
}

Usage:

System.out.println(getComponentTypeOfMultidimensionalArray(int[][].class)); // int

huangapple
  • 本文由 发表于 2020年9月12日 11:54:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63856657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定