Java 尺寸和长度方法之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20192843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between size and length methods?
提问by BeLambda
What is the difference between .size()
and .length
? Is .size()
only for arraylists and .length
only for arrays?
.size()
和 和有.length
什么区别?是.size()
仅适用于的ArrayList和.length
只为数组?
采纳答案by MattPutnam
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.
size()
是在 中指定的方法java.util.Collection
,然后被标准库中的每个数据结构继承。 length
是任何数组上的字段(数组是对象,您只是通常看不到类),并且length()
是 on 的方法java.lang.String
,char[]
无论如何它只是一个薄包装。
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.
也许按照设计,Strings 是不可变的,并且所有顶级 Collection 子类都是可变的。所以你看到“长度”的地方你知道它是恒定的,而你看到“大小”的地方不是。
回答by hsz
I bet (no language specified) size()
method returns length
property.
我打赌(没有指定语言)size()
方法返回length
属性。
However valid for
loop should looks like:
但是有效的for
循环应该如下所示:
for (int i = 0; i < values.length; i++) {}
回答by Buddha
Based on the syntax I'm assuming that it is some language which is descendant of C. As per what I have seen, length
is used for simple collection items like arrays and in most cases it is a property.
基于语法,我假设它是某种语言,它是 C 的后代。根据我所见,length
用于简单的集合项,如数组,在大多数情况下它是一个属性。
size()
is a function and is used for dynamic collection objects. However for all the purposes of using, you wont find any differences in outcome using either of them. In most implementations, size simply returns length property.
size()
是一个函数,用于动态集合对象。但是,就所有使用目的而言,使用它们中的任何一个都不会发现结果有任何差异。在大多数实现中,size 只返回长度属性。
回答by Ramakrishnakcr Kcr
length is constant which is used to find out the array storing capacity not the number of elements in the array
长度是常数,用于找出数组存储容量而不是数组中的元素数
Example:
例子:
int[] a = new int[5]
a.length
always returns 5, which is called the capacity of an array. But
a.length
始终返回 5,这称为数组的容量。但
number of elements in the array is called size
数组中元素的数量称为大小
Example:
例子:
int[] a = new int[5]
a[0] = 10
Here the size would be 1, but a.length
is still 5. Mind that there is no actual property or method called size
on an array so you can't just call a.size
or a.size()
to get the value 1.
这里的大小是 1,但a.length
仍然是 5。请注意,没有size
在数组上调用实际的属性或方法,因此您不能只调用a.size
或a.size()
获取值 1。
The size()
method is available for collections, length
works with arrays in Java.
该size()
方法可用于集合,length
适用于 Java 中的数组。
回答by Khan
length variable:
In Java, array (not java.util.Array) is a predefined class in the language itself. To find the elements of an array, designers used length variable (length is a field member in the predefined class). They must have given length() itself to have uniformity in Java; but did not. The reason is by performance, executing length variable is speedier than calling the method length(). It is like comparing two strings with == and equals(). equals() is a method call which takes more time than executing == operator.
在 Java 中,数组(不是 java.util.Array)是语言本身中的预定义类。为了查找数组的元素,设计者使用了长度变量(长度是预定义类中的字段成员)。他们必须给定 length() 本身才能在 Java 中具有一致性;但没有。原因在于性能,执行 length 变量比调用方法 length() 更快。这就像用 == 和 equals() 比较两个字符串。equals() 是一个方法调用,它比执行 == 操作符花费更多的时间。
size() method:
It is used to find the number of elements present in collection classes. It is defined in java.util.Collection interface.
它用于查找集合类中存在的元素数量。它在 java.util.Collection 接口中定义。
回答by TobiDoe
.length
is a field, containing the capacity(NOT the number of elements the array contains at the moment) of arrays.length()
is a methodused by Strings(amongst others), it returns the number of chars in the String; with Strings, capacity and number of containing elements (chars) have the same value.size()
is a methodimplemented by all members of Collection(lists, sets, stacks,...). It returns the number of elements(NOT the capacity; some collections even don′t have a defined capacity) the collection contains.
.length
是一个字段,包含容量(NOT此刻的数组包含的元素数)阵列。length()
是Strings使用的一种方法(除其他外),它返回String 中的字符数;对于字符串,包含元素(字符)的容量和数量具有相同的值。size()
是一种方法,通过实施收集所有成员(列表,集合,栈,...)。它返回集合包含的元素数量(不是容量;有些集合甚至没有定义的容量)。