Turn string values into 2d array of type double

前端 未结 5 883
失恋的感觉
失恋的感觉 2021-01-20 00:01

I have a string:

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

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

d         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-01-20 00:25

    What about something like this:

    Arrays.stream("0,4.28 10,4.93 20,3.75".split(" ")) //Stream
         .map(s -> 
               Arrays.stream(s.split(",")) // take an individual string like 0,4.28  
                     .map(Double::parseDouble) // and transform it to a double array
                     .toArray(Double[]::new)
          )
         .toArray(Double[][]::new);
    

    the result is

    $8 ==> Double[3][] { 
            Double[2] { 0.0, 4.28 }, 
            Double[2] { 10.0, 4.93 }, 
            Double[2] { 20.0, 3.75 } 
    }
    

提交回复
热议问题