Java array.length方法

我收到错误:未定义的符号:长度。

import java.util.*; class ssss { public static void main(String args[]) { int a[]={1,2,3,4}; System.out.println(a[1]); int x = a.length(); System.out.println(x); } } 

length是数组的字段,而不是方法。 使用a.length

@TheLostMind回答

更改

  int x = a.length(); 

 int x = a.length; 

length()不是有效的方法。 所以你可以这样试试

 class ssss { public static void main(String args[]) { int a[]={1,2,3,4}; System.out.println(a[1]); int x = a.length; System.out.println(x); } } 

length是public属性,而不是数组中的方法。 因此,我们访问数组的长度为:

 int x = a.length; 

而不是,

 int x = a.length();