Java String 数组:有方法的大小吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/921384/
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
Java String array: is there a size of method?
提问by phill
I come from a php background and in php, there is an array_size()
function which tells you how many elements in the array are used.
我来自 php 背景,在 php 中有一个array_size()
函数可以告诉你数组中有多少元素被使用。
Is there a similar method for a String[]
array? Thanks.
String[]
数组有没有类似的方法?谢谢。
采纳答案by Kris
Yes, .length
(property-like, not a method):
是的,.length
(类似于属性,而不是方法):
String[] array = new String[10];
int size = array.length;
回答by jjnguy
array.length
It is actually a final member of the array, not a method.
它实际上是数组的最终成员,而不是方法。
回答by dfa
array.length final property
array.length 最终属性
it is public and final property. It is final because arrays in Java are immutable by size (but mutable by element's value)
它是公共的和最终的财产。它是最终的,因为 Java 中的数组在大小上是不可变的(但在元素的值上是可变的)
回答by willcodejavaforfood
Arrays are objects and they have a length field.
数组是对象,它们有一个长度字段。
String[] haha = {"olle", "bulle"};
String[] haha = {"olle", "bulle"};
haha.length would be 2
haha.length 为 2
回答by bruno conde
In java there is a length
field that you can use on any array to find out it's size:
在java中有一个length
字段,你可以在任何数组上使用它来找出它的大小:
String[] s = new String[10];
System.out.println(s.length);
回答by Simon Groenewolt
Not really the answer to your question, but if you want to have something like an array that can grow and shrink you should not use an array in java. You are probably best of by using ArrayListor another List implementation.
不是你问题的真正答案,但如果你想要一个可以增长和缩小的数组,你不应该在 java 中使用数组。您可能最好使用ArrayList或其他 List 实现。
You can then call size() on it to get it's size.
然后你可以调用 size() 来获取它的大小。
回答by Tim H
The answer is "All of them". A java array is allocated with a fixed number of element slots. The "length" attribute will tell you how many. That number is immutable for the life of the array. For a resizable equivalent, you need one of the java.util.List classes - where you can use the size() method to find out how many elements are in use.
答案是“所有这些”。java 数组分配有固定数量的元素槽。“长度”属性会告诉你有多少。该数字在数组的生命周期内是不可变的。对于可调整大小的等效项,您需要 java.util.List 类之一 - 您可以在其中使用 size() 方法找出正在使用的元素数量。
However, there's "In use" and then there's In Use. In an class object array, you can have element slots whose elements are null objects, so even though they count in the length attribute, but most people's definitions, they're not in use (YMMV, depending on the application). There's no builtin function for returning the null/non-null counts.
然而,有“使用”,然后有在使用。在类对象数组中,您可以拥有元素为空对象的元素槽,因此即使它们计入长度属性,但大多数人的定义中,它们并未被使用(YMMV,取决于应用程序)。没有用于返回空/非空计数的内置函数。
List objects have yet another definition of "In Use". To avoid excessive creation/destruction of the underlying storage structures, there's typically some padding in these classes. It's used internally, but isn't counted in the returned size() method. And if you attempt to access those items without expanding the List (via the add methods), you'll get an illegal index exception.
列表对象还有另一个“使用中”的定义。为了避免过度创建/破坏底层存储结构,这些类中通常会有一些填充。它在内部使用,但不计入返回的 size() 方法。如果您尝试在不扩展 List 的情况下访问这些项目(通过 add 方法),您将获得非法索引异常。
So for Lists, you can have "In Use" for non-null, committed elements, All committed elements (including null elements), or All elements, including the expansion space presently allocated.
因此,对于列表,您可以为非空、已提交元素、所有已提交元素(包括空元素)或所有元素(包括当前分配的扩展空间)设置“正在使用”。
回答by Peter Lawrey
If you want a function to do this
如果你想要一个函数来做到这一点
Object array = new String[10];
int size = Array.getlength(array);
This can be useful if you don't know what type of array you have e.g. int[], byte[] or Object[].
如果您不知道您拥有什么类型的数组,例如 int[]、byte[] 或 Object[],这会很有用。
回答by vidyasagar
All the above answers are proper. The important thing to observe is arrays have length attribute but not length method. Whenever you use strings and arrays in java the three basic models you might face are:
以上答案都对。需要注意的重要一点是数组具有长度属性但没有长度方法。每当您在 Java 中使用字符串和数组时,您可能面临的三个基本模型是:
- String s=new String("vidyasagar");
System.out.println(s.length()); // In this case we are using only String. No length attribute for Strings. we have to use length() method. - int[] s=new int[10]; System.out.println(s.length); //here we use length attribute of arrays.
- String[] s=new String[10];
System.out.println(s.length); // Here even though data type is String, it's not a single String. s is a reference for array of Strings. So we use length attribute of arrays to express how many strings can fit in that array.
- String s=new String("vidyasagar");
System.out.println(s.length()); // 在这种情况下,我们只使用字符串。字符串没有长度属性。我们必须使用 length() 方法。 - int[] s=new int[10]; System.out.println(s.length); //这里我们使用数组的长度属性。
- 字符串[] s=新字符串[10];
System.out.println(s.length); // 这里即使数据类型是String,也不是单个String。s 是字符串数组的引用。因此,我们使用数组的长度属性来表示该数组中可以容纳多少个字符串。
回答by Liang
Also, it's probably useful to note that if you have a multiple dimensional Array, you can get the respective dimension just by appending a '[0]' to the array you are querying until you arrive at the appropriate axis/tuple/dimension.
此外,如果您有一个多维数组,则可能很有用,只需将“[0]”附加到您正在查询的数组中,直到到达适当的轴/元组/维度,您就可以获得相应的维度。
This is probably better explained with the following code:
这可能用以下代码更好地解释:
public class Test {
public static void main(String[] args){
String[][] moo = new String[5][12];
System.out.println(moo.length); //Prints the size of the First Dimension in the array
System.out.println(moo[0].length);//Prints the size of the Second Dimension in the array
}
}
Which produces the output:
产生输出:
5
12