多维数组的长度java的反思 [英] Multi-Dimension length array reflection java
本文介绍了多维数组的长度java的反思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要如何找到一个多维数组与Java反射的长度?
How do I find the length of a multi-dimensional array with reflection on java?
推荐答案
Java数组有每个实例的长度,而不是在同一维度所有数组必须有相等的长度。这就是说,你可以得到实例的长度中。
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.
尺寸可以通过在他们的名字[的数目进行计数,这比降类型层次更快。下面code:
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
这篇关于多维数组的长度java的反思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文