尺寸和长度方法的区别? [英] Difference between size and length methods?
问题描述
.size()
和 .length
之间有什么区别?是 .size()
仅适用于arraylists而 .length
仅适用于数组?
What is the difference between .size()
and .length
? Is .size()
only for arraylists and .length
only for arrays?
如果是这样的循环,例如:
If so for loops like:
for (int i=0; i <= values.length-1; i++)
与
for (int i=1; i <= values.length; i++)
推荐答案
size()
是<$ c $中指定的方法c> 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, arrays and 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屋!