How do I find the length of a multi-dimensional array with reflection on java?
如何在java上找到带有反射的多维数组的长度?
5 个解决方案
#1
9
Java arrays have lengths per instance, not all arrays in the same dimension have to have equals lengths. That said, you can get the lengths of instances in the.
Java数组在每个实例中都有长度,并非同一维度中的所有数组都必须具有等长。也就是说,你可以获得实例的长度。
Dimensions can be counted by the number of '[' in their name, this is quicker than descending the type hierarchy. The following code:
维度可以通过名称中的“[”数来计算,这比降序类型层次更快。以下代码:
int[][][] ary = {{{0},{1}}};
Class cls = ary.getClass();
boolean isAry = cls.isArray();
String clsName = cls.getName();
System.out.println("is array=" + isAry);
System.out.println("name=" + clsName);
int nrDims = 1 + clsName.lastIndexOf('[');
System.out.println("nrDims=" + nrDims);
Object orly = ary;
for (int n = 0; n < nrDims; n++) {
int len = Array.getLength(orly);
System.out.println("dim[" + n + "]=" + len);
if (0 < len) {
orly = Array.get(orly, 0);
}
}
gives the following output:
给出以下输出:
is array=true
name=[[[I
nrDims=3
dim[0]=1
dim[1]=2
dim[2]=1
#2
12
There is no such thing as "length" for multi-dimensional array; it may not be rectangular. I'm guessing you're talking about the number of dimensions. You need to descend into it iteratively and count.
多维数组没有“长度”这样的东西;它可能不是矩形的。我猜你在谈论尺寸的数量。你需要迭代地进入它并计数。
public int getDimensionCount(Object array) {
int count = 0;
Class arrayClass = array.getClass();
while ( arrayClass.isArray() ) {
count++;
arrayClass = arrayClass.getComponentType();
}
return count;
}
#3
0
Class clazz = Class.forName("ReflectionTest");
Method m = clazz.getDeclaredMethod("getArray");
Object o1 = m.invoke(o, arg);
int array[][] = (int[][])o1;
System.out.println("Array length: " + array.length);
System.out.println("Array length: " + array[0].length);
#4
0
Use java.lang.reflect.Array.getLength(obj)
.
使用java.lang.reflect.Array.getLength(obj)。
#5
0
If you like me were trying to get the number of dimensions and the size of them then:
如果你喜欢我试图获得尺寸和尺寸,那么:
private static int[] getDimentionsOf(final Object expectedArray) {
if (!expectedArray.getClass().isArray()) {
return new int[0];
}
final int dimensionSize = Array.getLength(expectedArray);
final int[] innerDimensions =
(expectedArray.getClass().getComponentType().isArray())
? getDimentionsOf(Array.get(expectedArray, 0))
: new int[0];
final int lenghtPlusOne = innerDimensions.length + 1;
final int[] newDimensions = new int[lenghtPlusOne];
System.arraycopy(innerDimensions, 0, newDimensions, 1, innerDimensions.length);
newDimensions[0] = dimensionSize;
return newDimensions;
}
#1
9
Java arrays have lengths per instance, not all arrays in the same dimension have to have equals lengths. That said, you can get the lengths of instances in the.
Java数组在每个实例中都有长度,并非同一维度中的所有数组都必须具有等长。也就是说,你可以获得实例的长度。
Dimensions can be counted by the number of '[' in their name, this is quicker than descending the type hierarchy. The following code:
维度可以通过名称中的“[”数来计算,这比降序类型层次更快。以下代码:
int[][][] ary = {{{0},{1}}};
Class cls = ary.getClass();
boolean isAry = cls.isArray();
String clsName = cls.getName();
System.out.println("is array=" + isAry);
System.out.println("name=" + clsName);
int nrDims = 1 + clsName.lastIndexOf('[');
System.out.println("nrDims=" + nrDims);
Object orly = ary;
for (int n = 0; n < nrDims; n++) {
int len = Array.getLength(orly);
System.out.println("dim[" + n + "]=" + len);
if (0 < len) {
orly = Array.get(orly, 0);
}
}
gives the following output:
给出以下输出:
is array=true
name=[[[I
nrDims=3
dim[0]=1
dim[1]=2
dim[2]=1
#2
12
There is no such thing as "length" for multi-dimensional array; it may not be rectangular. I'm guessing you're talking about the number of dimensions. You need to descend into it iteratively and count.
多维数组没有“长度”这样的东西;它可能不是矩形的。我猜你在谈论尺寸的数量。你需要迭代地进入它并计数。
public int getDimensionCount(Object array) {
int count = 0;
Class arrayClass = array.getClass();
while ( arrayClass.isArray() ) {
count++;
arrayClass = arrayClass.getComponentType();
}
return count;
}
#3
0
Class clazz = Class.forName("ReflectionTest");
Method m = clazz.getDeclaredMethod("getArray");
Object o1 = m.invoke(o, arg);
int array[][] = (int[][])o1;
System.out.println("Array length: " + array.length);
System.out.println("Array length: " + array[0].length);
#4
0
Use java.lang.reflect.Array.getLength(obj)
.
使用java.lang.reflect.Array.getLength(obj)。
#5
0
If you like me were trying to get the number of dimensions and the size of them then:
如果你喜欢我试图获得尺寸和尺寸,那么:
private static int[] getDimentionsOf(final Object expectedArray) {
if (!expectedArray.getClass().isArray()) {
return new int[0];
}
final int dimensionSize = Array.getLength(expectedArray);
final int[] innerDimensions =
(expectedArray.getClass().getComponentType().isArray())
? getDimentionsOf(Array.get(expectedArray, 0))
: new int[0];
final int lenghtPlusOne = innerDimensions.length + 1;
final int[] newDimensions = new int[lenghtPlusOne];
System.arraycopy(innerDimensions, 0, newDimensions, 1, innerDimensions.length);
newDimensions[0] = dimensionSize;
return newDimensions;
}