62,621
社区成员
发帖
与我相关
我的任务
分享import java.lang.reflect.*;
public class Test {
public static void main(String[] args) throws Throwable {
int[][][] a = {{{},{0}}, {{1,2}, {3,4,5}}};
System.out.println(elementCount(a));
}
public static int elementCount(Object o) {
int count = 0;
if (o != null) {
Class<?> cls = o.getClass();
if (cls.getName().startsWith("[[")) {
for (int i=0; i<Array.getLength(o); i++) {
count += elementCount(Array.get(o, i));
}
} else if (cls.isArray()) {
count += Array.getLength(o);
}
}
return count;
}
}