Turn string values into 2d array of type double

前端 未结 5 882
失恋的感觉
失恋的感觉 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:24

    I would go step by step resolving this task.

    First, I would split the original String by a space, then split the results by comma each and afterwards create an array of double out of those values with Double.parseDouble(String value).

    public static void main(String[] args) {
        String stringProfile = "0,4.28 10,4.93 20,3.75";
    
        // split it once by space
        String[] parts = stringProfile.split(" ");
    
        // create some result array with the amount of double pairs as its dimension
        double[][] results = new double[parts.length][];
    
        // iterate over the result of the first splitting
        for (int i = 0; i < parts.length; i++) {
            // split each one again, this time by comma
            String[] values = parts[i].split(",");
    
            // create two doubles out of the single Strings
            double a = Double.parseDouble(values[0]);
            double b = Double.parseDouble(values[1]);
    
            // add them to an array
            double[] value = {a, b};
    
            // add the array to the array of arrays
            results[i] = value;
        }
    
        // then print the result
        for (double[] pair : results) {
            System.out.println(String.format("%.0f and %.2f", pair[0], pair[1]));
        }
    }
    

    Yes, these are a lot of lines of code, but most likely more easily understandable than lambda expressions (which are cooler and more elegant in my opinion).

    0 讨论(0)
  • 2021-01-20 00:25

    What about something like this:

    Arrays.stream("0,4.28 10,4.93 20,3.75".split(" ")) //Stream<String>
         .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 } 
    }
    
    0 讨论(0)
  • 2021-01-20 00:29

    If your string follows the pattern you described then you do it like this:

    String stringProfile = "0,4.28 10,4.93 20,3.75";
    String[] split = stringProfile.split(" "); // split by space;
    
    double[][] a = new double[split.length][]; // your result
    
    for(int i = 0; i < split.length; i++) {
        String[] numbers = split[i].split(","); // split by ,
        double[] doubles = Arrays.stream(numbers).mapToDouble(Double::new).toArray(); //create 1-D array
        a[i] = doubles; // assign it do your result
    }
    
    0 讨论(0)
  • 2021-01-20 00:41

    Assuming that your string strictly follows the given sample pattern, you can make use of the following code:

        String stringProfile = "0,4.28 10,4.93 20,3.75";
        stringProfile = stringProfile.replace(' ', ',');
        String [] strArry = stringProfile.split(",");
        double [][] doubleArray = new double [strArry.length/2][2];
        for(int i=0, j=0; i<strArry.length; i=i+2, j++) {
            doubleArray [j][0] = Double.valueOf(strArry[i]);
            doubleArray [j][1] = Double.valueOf(strArry[i+1]);
        }
    
    0 讨论(0)
  • 2021-01-20 00:45

    if you split with " " you get an array of one dimension, then you can split again and get a multidimensional array like you want:

    String arrayS = "0,4.28 10,4.93 20,3.75";
    String [] a = arrayS.spit(" ");
    double [][] arrayD;
    for(String j: a){
        arrayD.append(j.split(","));
    }
    //then print your array here
    
    0 讨论(0)
提交回复
热议问题