我的背景是php,在php中,有一个array_size()
函数,它告诉你数组中使用了多少元素。
对于String[]
数组,是否有类似的方法?谢谢。
发布于 2009-05-28 15:17:26
是的,.length
(类似于属性,不是一个方法):
String[] array = new String[10];
int size = array.length;
发布于 2009-05-28 15:17:16
array.length
它实际上是数组的最后一个成员,而不是方法。
发布于 2011-12-31 06:23:30
另外,注意如果你有一个多维的Array,你可以通过将'‘附加到你要查询的数组,直到到达合适的轴/元组/维数来获得相应的维数。
下面的代码可能更好地解释了这一点:
public class Test {
public static void main(String[] args){
String[][] moo = new String[5][12];
System.out.println(moo.length); //Prints the size of the First Dimension in the array
System.out.println(moo[0].length);//Prints the size of the Second Dimension in the array
}
}
它会产生输出:
5
12
https://stackoverflow.com/questions/921384
复制相似问题