将字符串转换为多维数组 [英] Turn string into multidimensional array

查看:152
本文介绍了将字符串转换为多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:

String stringProfile = "0, 4.28 10, 4.93 20, 3.75";

我试图把它变成一个如下数组:

I am trying to turn it into an array like as follows:

double [][] values = {{FastMath.toRadians(0), FastMath.toRadians(4.28)},
                      {FastMath.toRadians(10), FastMath.toRadians(4.93)},
                      {FastMath.toRadians(20), FastMath.toRadians(3.75)}}; 

其中 FastMath.toRadians 是一个方法每个元素。

Where FastMath.toRadians is a method on each element.

问题数量:

首先要做的事情就是拆分字符串:

First logical thing to do is to split the string:

List<String> stringProfileList= Arrays.asList(stringProfile.split(" "));

输出为0,4.28,10,4.93,3, 3.75,所以现在每个元素都被,而不是其他元素分开。

The output would be "0, 4.28, 10, 4.93, 20, 3.75", so now every element is split by a "," rather than every other.

然后对于列表中的每2个元素,我需要分配一个数组。
在每个数组中,我在每个元素上应用FastMath.toRadians(),然后将每个数组附加到一个更大的多维数组。

Then for every 2 elements in the list, I need to assign to an array. Within each Array I apply the FastMath.toRadians() on each element and then I append each array to a larger multidimensional array.

这是正确的方法这个怎么样?我正在坚持实现每2个元素。

Is this the right way to go about this? I'm getting stuck at implementing for every 2 elements.

我还需要将这些字符串元素转换为double,这似乎并不重要。

I also need to convert these string elements into a double, which doesn't seem to trivial.

推荐答案

如果你想使用 Stream 试试这个:

If you want to use Stream try this:

    Pattern PAIR = Pattern.compile("(?<=\\d)\\s(?=\\d)");

    double[][] result = PAIR.splitAsStream(stringProfile)
            .map(pair -> pair.split(","))
            .map(pair -> new double[] { FastMath.toRadians(Double.valueOf(pair[0])), FastMath.toRadians(Double.valueOf(pair[1])) })
            .toArray(i -> new double[i][]);

A 模式用于拆分 stringProfile 在空白字符上有后面的数字提前它们。这会产生一个 Stream 的字符串,其中包含一对数字除以','。我们拆分第一个地图中的','上的这个字符串获取表示数字的字符串数组。在下一个 map 中,解析的数字和它们的弧度被计算出来。

A Pattern is used to split stringProfile on whitespace characters which have a digit behind and ahead them. This results in a Stream of strings which contains a pair of numbers divided by ', '. We split this string on ',' in a first map to get an array of strings representing the numbers. In the next map the numbers a parsed and their radians is computed.

我们可以解析数字以及弧度到单个 map 步骤的共同点。以下代码更详细,但创建了一个额外的双数组:

We could devide the parsing of the numbers and the comuputation of the radians to single map steps. The following code is more verbose but creates an additional double array:

double[][] result = PAIR.splitAsStream(stringProfile)
        .map(pair -> pair.split(","))
        .map(pair -> new double[] { Double.valueOf(pair[0]), Double.valueOf(pair[1]) })
        .map(pair -> new double[] { FastMath.toRadians(pair[0]), FastMath.toRadians(pair[1]) })
        .toArray(i -> new double[i][]);

如果我们想重用双数组,我们可以这样做:

If we want to reuse the double array, we can do this like this:

        .map(pair -> {
            pair[0] = FastMath.toRadians(pair[0]);
            pair[1] = FastMath.toRadians(pair[1]);
            return pair;
        })

最后一步将双数组收集到一个二维数组中。

The last step collects the double arrays into a two dimensional array.

编辑:

查看此演示

使用模式类使用的模式查找空白字符( \\\\ )之前((?< = \\d) = 看后面)并成功((?= \\d) = 展望未来)电子数字( \\\\ )。这些外观具有零宽度,因此它们被评估为捕获(匹配)但不是匹配序列的一部分( Matcher.group( ) )。 匹配序列仅包含空格字符( \\\\ )。

Using the wording of the documentation of the Pattern class the used pattern looks for a whitespace character (\\s) which is preceded ((?<=\\d) = look behind) and succeeded ((?=\\d) = look ahead) by one digit (\\d). These looks have zero-width, thus they are evaluated for capturing (matching) but are not part of the matched sequence (Matcher.group()). The matching sequence contains only a whitespace character (\\s).

对于 stringProfile 这种模式在两种情况下匹配:8 13 2。在这些情况下,两个数字围绕。另一方面,例如0,4不匹配。 的出现会阻止匹配。

For stringProfile this pattern will match in two cases: "8 1" and "3 2". In these cases two digits are surrounding a " ". On the other hand e.g. "0, 4" doesn't match. The occurrence of "," prevents a match.

这篇关于将字符串转换为多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆