line.split(",")[1] 是什么意思 [Java]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36403986/
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-11-03 01:24:35  来源:igfitidea点击:

What does line.split(",")[1] mean [Java]?

javastringdoublevalue-of

提问by SmashCode

I came across code where i had encountered with Double.valueOf(line.split(",")[1])I am familiar with Double.valueOf()and my problem is to understand what does [1]mean in the sentence. Searched docs didn't find anything.

我遇到过Double.valueOf(line.split(",")[1])我熟悉的代码,Double.valueOf()我的问题是理解[1]句子中的含义。搜索文档没有找到任何内容。

while ((line = reader.readLine()) != null)
                double crtValue = Double.valueOf(line.split(",")[1]);

回答by dryairship

It means that your lineis a String of numbers separated by commas.
eg: "12.34,45.0,67.1"

这意味着您line是一个由逗号分隔的数字字符串。
例如:"12.34,45.0,67.1"

The line.split(",")returns an array of Strings.
eg: {"12.34","45.0","67.1"}

line.split(",")返回字符串的数组。
例如:{"12.34","45.0","67.1"}

line.split(",")[1]returns the 2nd(because indexes begin at 0) item of the array.
eg: 45.0

line.split(",")[1]返回数组的第二个(因为索引从 0 开始)项。
例如:45.0

回答by dejvuth

It means lineis a string beginning with a,bwhere bis in fact a number.

这意味着line是一个以a,bwhere开头的字符串b,实际上是一个数字。

crtValueis the doublevalue of b.

crtValue是 的doubleb

回答by Buhake Sindi

Java public String[] split(String regex)

爪哇 public String[] split(String regex)

Splits this string around matches of the given regular expression.

围绕给定正则表达式的匹配项拆分此字符串。

It

Returns:the array of stringscomputed by splitting this string around matches of the given regular expression

返回:通过围绕给定正则表达式的匹配拆分此字符串计算出的字符串数组

So the [1]gets the 2nd item of the array found in String[].

因此,[1]获取 中找到的数组的第二项String[]

回答by Andrew Tobilko

Your code tries to get the second doublevalue from reader.readLine().

您的代码尝试doublereader.readLine().



  1. String numbers = "1.21,2.13,3.56,4.0,5";
  2. String[] array = numbers.split(",");split the input line by commma
  3. String second = array[1];get the second element from the array. Java array numeration starts from 0index.
  4. double crtValue = Double.valueOf(second);convert Stringto double
  1. String numbers = "1.21,2.13,3.56,4.0,5";
  2. String[] array = numbers.split(",");用逗号分割输入行
  3. String second = array[1];从数组中获取第二个元素。Java 数组编号从0索引开始。
  4. double crtValue = Double.valueOf(second);转换Stringdouble


Don't forget about NumberFormatExceptionthat may be thrown if the string does not contain a parsable double.

不要忘记NumberFormatException,如果字符串不包含 parsable 可能会抛出double