java Java将一串浮点数解析为一个浮点数组?

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

Java parsing a string of floats to a float array?

javaarraysstringparsingfloating-point

提问by Sonoman

Is there a simple way to parse a string of floats to a float array? I'm writing an importer which needs to parse an ascii file to get some values out and I'm just wondering if there's a simpler way to do this then search for all the whitespace myself and use Float.parseFloat(s)for each whitespace-separated value.

有没有一种简单的方法可以将一串浮点数解析为一个浮点数数组?我正在编写一个需要解析 ascii 文件以获取一些值的导入器,我只是想知道是否有一种更简单的方法可以做到这一点,然后自己搜索所有空格并Float.parseFloat(s)用于每个空格分隔的值。

For example, the string is

例如,字符串是

1 0 4 0 26 110.78649609798859 39 249.34908705094128 47 303.06802752888359

1 0 4 0 26 110.78649609798859 39 249.34908705094128 47 303.06802752888359

I want to create an array of floats as:

我想创建一个浮点数组:

[1, 0, 4, 0, 26, 110.78649609798859, 39, 249.34908705094128, 47, 303.06802752888359]

[1, 0, 4, 0, 26, 110.78649609798859, 39, 249.34908705094128, 47, 303.06802752888359]

Thanks for the help!

谢谢您的帮助!

回答by Reese Moore

Use a Scanner [API]

使用扫描仪 [ API]

Use the Scanner#hasNextFloatand Scanner#nextFloatmethods to loop through and get all of the floats and build them into an array.

使用Scanner#hasNextFloatScanner#nextFloat方法循环并获取所有浮点数并将它们构建到数组中。

回答by Jason Rogers

You can do like this

你可以这样做

Split the String

拆分字符串

String[] split(String regex) Splits this string around matches of the given regular expression.

String[] split(String regex) 围绕给定正则表达式的匹配项拆分此字符串。

String[] tabOfFloatString = bigStringWithAllFloats.split(regex);

regex can be space, tab, comma whatever (advantage of regex is you can combine all you want, I use this in my xml reader "[-+.,:;]" ); then loop on that and convert to floats

正则表达式可以是空格、制表符、逗号等(正则表达式的优点是你可以组合所有你想要的,我在我的 xml 阅读器中使用它 "[-+.,:;]" );然后循环并转换为浮点数

for(String s : tabOfFloatString){
    float res = Float.parseFloat(s);
    //do whatever you want with the float
}

回答by khachik

回答by G B

If you just want an array of float, the easy way is to split the string:

如果你只想要一个浮点数组,简单的方法是拆分字符串:

String[] flostr = myString.split(" ");
float[] floats = new float[flostr.length];

and then iterate on the string array, and parse the single float values.

然后迭代字符串数组,并解析单个浮点值。

Alternatively, you could use a Scanner or a StringTokenizer, put all values into a List and create an array at the end.

或者,您可以使用 Scanner 或 StringTokenizer,将所有值放入一个 List 并在最后创建一个数组。

回答by Sean Patrick Floyd

Here's a Guavasolution, but I'm surprised at how complicated it apparently needs to be. Perhaps someone can give me a hint as how to shorten it:

这是一个番石榴解决方案,但我很惊讶它显然需要多么复杂。也许有人可以给我一个关于如何缩短它的提示:

public static float[] extractFloats(final String input){
    // check for null Strings
    Preconditions.checkNotNull(input);
    return Floats.toArray(Lists.transform(
        Lists.newArrayList(Splitter.on(Pattern.compile("\s+")).split(
            input.trim())), new Function<String, Float>(){

            @Override
            public Float apply(final String input){
                return Float.valueOf(input);
            }
        }));
}

Test Code:

测试代码:

String param =
    "1 0 4 0 26 110.78649609798859 39 249.34908705094128 47 303.06802752888359";
float[] floats = extractFloats(param);
System.out.println(Arrays.toString(floats));

Output:

输出:

[1.0, 0.0, 4.0, 0.0, 26.0, 110.7865, 39.0, 249.34909, 47.0, 303.06802]

[1.0, 0.0, 4.0, 0.0, 26.0, 110.7865, 39.0, 249.34909, 47.0, 303.06802]