Java String a method for converting a double two-dimensional array _java

Source: Internet
Author: User

WHY

Friends in the group to ask for a question, the problem prototype is this:

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} 
    }

A two-dimensional array that converts a string that can be converted into a double.

How

At first glance, it feels so simple, it's implemented, and it's customary to paste the code first:

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]);
			}
		

Here are a few points to be aware of

1. Replace the curly braces with the brackets first,

2. Using JSON transformation, first turn into a string two-dimensional array

3. Then the value in the array is converted to a double

4. The JSON package I use is Fastjson.

Import Com.alibaba.fastjson.JSON;

The above is the small series for everyone to bring Java String into a double two-dimensional array of methods all the content, I hope that we support the cloud Habitat Community ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.