你如何确定字符串数组java的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18949774/
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
How do you determine size of string array java?
提问by randomname
How do i read a file and determine the # of array elements without having to look at the text file itself?
如何读取文件并确定数组元素的数量而不必查看文本文件本身?
String temp = fileScan.toString();
String[] tokens = temp.split("[\n]+");
// numArrayElements = ?
采纳答案by Konstantin Yovkov
Use the length
property of the array:
使用length
数组的属性:
int numArrayElements = tokens.length;
回答by tbodt
The proper expression is tokens.length
. So, you can assign numArrayElements
like this:
正确的表达是tokens.length
。所以,你可以这样分配numArrayElements
:
int numArrayElements = tokens.length;
This counts the number of elements in the tokens
array. You can count the number of elements in any array in the same way.
这会计算tokens
数组中元素的数量。您可以以相同的方式计算任何数组中的元素数。