尺寸和长度方法的区别? [英] Difference between size and length methods?
问题描述
.size()
和 .length
有什么区别?.size()
仅用于数组列表而 .length
仅用于数组吗?
What is the difference between .size()
and .length
? Is .size()
only for arraylists and .length
only for arrays?
推荐答案
size()
是 java.util.Collection
中指定的一个方法,然后被继承标准库中的每个数据结构.length
是任何数组上的一个字段(数组是对象,你只是通常看不到这个类),而 length()
是 java 上的一个方法.lang.String
,无论如何它只是 char[]
的一个薄包装.
size()
is a method specified in java.util.Collection
, which is then inherited by every data structure in the standard library. length
is a field on any array (arrays are objects, you just don't see the class normally), and length()
is a method on java.lang.String
, which is just a thin wrapper on a char[]
anyway.
也许是设计使然,字符串是不可变的,并且所有顶级 Collection 子类都是可变的.所以你看到长度"的地方你知道它是恒定的,而你看到大小"的地方不是.
Perhaps by design, Strings are immutable, and all of the top-level Collection subclasses are mutable. So where you see "length" you know that's constant, and where you see "size" it isn't.
这篇关于尺寸和长度方法的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!