Java 将字符串格式的数组转换为数组

Java 将字符串格式的数组转换为数组,java,arrays,Java,Arrays,我有以下字符串: "[[0, 0, 0], [1, 1, 1], [2, 2, 2]]" 在Java中,将其转换为纯多维浮点数组的最简单方法是什么? 例如,像这样的事情: String stringArray = "[[0, 0, 0], [1, 1, 1], [2, 2, 2]]"; float[][] floatArray = stringArray.parseSomeHow() //here I don't know the best way to convert 当然,我可以写一个算

我有以下字符串:

"[[0, 0, 0], [1, 1, 1], [2, 2, 2]]"
在Java中,将其转换为纯多维浮点数组的最简单方法是什么? 例如,像这样的事情:

String stringArray = "[[0, 0, 0], [1, 1, 1], [2, 2, 2]]";
float[][] floatArray = stringArray.parseSomeHow() //here I don't know the best way to convert
当然,我可以写一个算法,例如读取每个字符左右。但也许java已经提供了一种更简单、更快的方法。

我脑海中浮现的“伪代码”:

1-删除第一个和最后一个字符(例如:删除第一个“[”和最后一个“]”)

2-用于查找括号之间的文本

3-循环步骤2的匹配项,并按“,”字符进行循环

4-在拆分的字符串上循环并修剪前面的值,然后将该值放入数组中的正确位置


代码示例

public static void main(String[] args) {
    String stringArray = "[[0, 0, 0], [1, 1, 1], [2, 2, 2]]";

    //1. Get rid of the first and last characters (e.g: remove the first "[" and the last "]").

    stringArray = stringArray.substring(1, stringArray.length() - 1);

    //2. Use regex to find the text between brackets.
    Pattern pattern = Pattern.compile("\\[(.*?)\\]");
    Matcher matcher = pattern.matcher(stringArray);

    //3. Loop over the matches of step 2 and split them by the "," character.
    //4.  Loop over the splitted String and trim the value before casting it into a float and then put that value in the array in the correct position.

    float[][] floatArray = new float[3][3];
    int i = 0;
    int j = 0;
    while (matcher.find()){
        String group = matcher.group(1);
        String[] splitGroup = group.split(",");
        for (String s : splitGroup){
            floatArray[i][j] = Float.valueOf(s.trim());
            j++;
        }
        j = 0;
        i++;
    }
    System.out.println(Arrays.deepToString(floatArray));
   //This prints out [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]
}

这里有一种实现方法:

public static float[][] toFloatArray(String s){
    String [] array = s.replaceAll("[\\[ ]+", "").split("],");

    float [][] floatArray = new float[array.length][];

    for(int i = 0; i < array.length; i++){
        String [] row = array[i].split("\\D+");
        floatArray[i] = new float[row.length];
        for(int j = 0; j < row.length; j++){
            floatArray[i][j] = Float.valueOf(row[j]);
        }           
    }

    return floatArray;
}

请在尝试提问之前阅读。请在尝试提问之前阅读。为什么不通过“stringArray”在实际的字符串数组中?在尝试提出更多基于意见的问题之前,请先阅读,这些问题会引起争论性的讨论,因为它们没有一个统一的答案。请阅读错误的提问方式。我想知道的是,在这种情况下是否有类似Integer.parse(someStringWithNumbers)的东西。但由于出现了多种算法,我知道不存在这样的事情。感谢您的回复。这是一个很好的解释,它需要一小段代码,您的答案将是完美的+1从我这里;)
public static Float[][] toFloatArray2(String s) {
    return Pattern.compile("[\\[\\]]+[,]?")
            .splitAsStream(s)
            .filter(x -> !x.trim().isEmpty())
            .map(row -> Pattern.compile("\\D+")
                        .splitAsStream(row)
                        .map(r -> Float.valueOf(r.trim()))
                        .toArray(Float[]::new)
            )
            .toArray(Float[][]::new);
}