Convert String[] with text to double[]

后端 未结 5 1093
旧巷少年郎
旧巷少年郎 2021-01-29 15:02

I have an Arraylist that I want to convert to a double[] array. I first convert the Arraylist to a String []. I then try to convert the String[] to a double[] but fail in the p

相关标签:
5条回答
  • 2021-01-29 15:31

    I would do this:

    • make array
    • convert to string

    then this code

    for (String s: list1) {
        boolean obtained = false;
        double tempD;
    
        try {
            tempD = Double.parseDouble(s); 
            obtained = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        if (obtained) {
            list2.add(tempD);
        }
    }
    
    0 讨论(0)
  • 2021-01-29 15:41

    Loop throug the Array:

    foreach String[]
        double[counter] = parseToDouble(String[counter])
    

    EDIT:
    Java:

       String[] str1 = list1.toArray(str1);
       double[] dou1 = new double[str1.length]
       for(int counter = 0; counter < str1.length;counter++)
          dou1[counter] = Double.parseDouble(str1[counter].replaceAll("RSP_\\d+",""));
    
    0 讨论(0)
  • 2021-01-29 15:44

    This will do the work.

    double[] doubles = new double[array1.length];
    for(int i=0; i<array1.length; i++){
       doubles[i] = Double.valueOf(array1[i])
    } 
    

    Thanks SmartLemon for the edit, you could do this too instead of using a String[]:

    double[] doubles = new double[list1.size()];
    for(int i=0; i<list1.size(); i++){
       doubles[i] = Double.valueOf(list1.get(i).replace("RSP_\\d+",""));
    }
    

    EDIT: So yes, you need a delimiter:

    double[] doubles = new double[array1.length*2]; //multiply by 2 because you really have 8 numbers not 4
    String [] array2 = null;
    
    for(String string: array1) { 
        array2 = string.split(",");
        for(int i=0; i<array2.length; i++){
            doubles[i] = Double.valueOf(array2[i]);
        }
    }
    

    this should do the work for sure.

    0 讨论(0)
  • 2021-01-29 15:50

    Use this

     String[] str1 = new String[list1.size()];
     str1 = list1.toArray(str1);
     double[] doubleArray = new double[str1.length]
     int i=0;
     for(String s:str1){
        doubleArray[i] = Double.valueOf(s.trim());
        i++;
     }
    
    0 讨论(0)
  • 2021-01-29 15:52

    You need to split each String in list1 on "," and attempt to parse each String that gets split out:

        ArrayList<Double[]> results = Lists.newArrayList();
        for( String s : list1 ) {
            String[] splitStrings = s.split(",");
            Double[] doublesForCurrentString = new Double[splitStrings.length];
            for(int i=0; i<splitStrings.length; i++){
                try {
                    doublesForCurrentString[i] = Double.valueOf(splitStrings[i]);
                } catch( NumberFormatException ex ) {
                    // No action.
                }
            }
            results.add(doublesForCurrentString);
        }
        Double[][] doubleArray = (Double[][])results.toArray();
    

    Crucial points:

    • EDIT: As @Tim Herold points out, you're probably better of performance-wise avoiding attempting to parse content you know to be non-numeric. In this case, I'd still split first and then just put in code that prevents you from attempting to parseDouble() on the first split String in each line, rather than trying to do String replacement before the split; that will be faster (and if we're not concerned about performance, then try/catch is perfectly fine and more readable). ORIGINAL: You need a try/catch when you try to parse the doubles, in case there's any invalid input. Bonus: you don't need to remove the non-numeric text now, you can just let this try/catch handle it.
    • Your strings have two doubles in each of them. You're not going to be able to just strip the text at the beginning and then parse the rest, because it's not going to be a valid double.
    • ArrayLists are generally easier to use; I'd opt for returning ArrayList<Double> (or ArrayList<ArrayList<Double>>) over Double[] or Double[][] any day of the week. There are certainly situations where I'd do differently, but yours doesn't sound like one of them to me.
    0 讨论(0)
提交回复
热议问题