你如何确定字符串数组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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:50:03  来源:igfitidea点击:

How do you determine size of string array java?

javaarraysstring

提问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 lengthproperty of the array:

使用length数组的属性:

int numArrayElements = tokens.length;

回答by tbodt

The proper expression is tokens.length. So, you can assign numArrayElementslike this:

正确的表达是tokens.length。所以,你可以这样分配numArrayElements

int numArrayElements = tokens.length;

This counts the number of elements in the tokensarray. You can count the number of elements in any array in the same way.

这会计算tokens数组中元素的数量。您可以以相同的方式计算任何数组中的元素数。