大家好,又见面了,我是你们的朋友全栈君。
首先区分一下length和length();
length不是方法,是属性,数组的属性;
public static void main(String[] args) {
int[] intArray = {1,2,3};
System.out.println("这个数组的长度为:" + intArray.length);
}
length()是字符串String的一个方法;
public static void main(String[] args) {
String str = "HelloWorld";
System.out.println("这个字符串的长度为:" + str.length());
}
进入length()方法看一下实现
private final char value[];
public int length() {
return value.length;
}
注释中的解释是
@return the length of the sequence of characters represented by this object.
即由该对象所代表的字符序列的长度,所以归根结底最后要找的还是length这个底层的属性;
size()方法,是List集合的一个方法;
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println("这个list的长度为:" + list.size());
}
在List的方法中,是没有length()方法的;
也看一段ArrayList的源码
private final E[] a;
ArrayList(E[] array) {
if (array==null)
throw new NullPointerException();
a = array;
}
public int size() {
return a.length;
}
由这段就可以看出list的底层实现其实就是数组,size()方法最后要找的其实还是数组的length属性;
另外,除了List,Set和Map也有size()方法,所以准确说size()方法是针对集合而言。
总结:
length——数组的属性;
length()——String的方法;
size()——集合的方法;
谨记。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147197.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有