数组有没有length()这个方法? String有没有length()这个方法?
答案:数组有length属性,而String有length()方法。
以下是实例代码:
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String st1 = new String("aaa");
System.out.println("the length of the string is: " + st1.length());
int ar1[]={1,2};
System.out.println("the length of the array is: " + ar1.length);
}
}
运行结果为:
the length of the string is: 3
the length of the array is: 2