WHY
My friend asked for help in the group, and the prototype of the problem is as follows:
String str = "{{10.14, 11.24, 44.55, 41.01},{12.10, 14.21, 52.14, 50.44},{14.44, 16.12, 45.42, 47.55}}";Convert to double[][]{ {10.14, 11.24, 44.55, 41.01}, {12.10, 14.21, 52.14, 50.44},{14.44, 16.12, 45.42, 47.55} }That is, convert a convertible String into a two-dimensional array that doubles.
HOW
At first glance, it feels very simple. I implemented it and post the code first as usual:
String str = "{{10.14, 11.24, 44.55, 41.01},{12.10, 14.21, 52.14, 50.44},{14.44, 16.12, 45.42, 47.55}}";str = str.replace("{", "[").replace("}", "]");String[][] arr = JSON.parseObject(str, String[][].class);Double[][] ds = new Double[arr.length][arr[0].length];for(int j=0;j<arr.length;j++){for(int i=0;i<arr[0].length;i++){ds[j][i] = Double.valueOf(arr[j][i]);}}There are a few things to note in this
1. First replace the curly braces with brackets.
2. Use JSON conversion and convert it into a two-dimensional array of String first
3. Then convert the value in the array into a Double
4. The JSON package I use is fastjson
import com.alibaba.fastjson.JSON;
The above is the full content of the method to convert java String into a Double two-dimensional array brought to you by the editor. I hope everyone will support Wulin.com more~