将带有文本的 String[] 转换为 double[] [英] Convert String[] with text to double[]

查看:27
本文介绍了将带有文本的 String[] 转换为 double[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要转换为 double[] 数组的 Arraylist.我首先将 Arraylist 转换为 String [].然后我尝试将 String[] 转换为 double[] 但在此过程中失败.问题是:字符串包含一些文本,以及一些带小数的数字.我只想将数字和小数转换为 double[] 数组并简单地删除文本.但是,我只知道如何使用字符串删除文本,而不知道如何使用字符串[].请看:

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 process. Here's the issue: the string contains some text, as well as some numbers with decimals. I want to convert only the numbers and decimals to a double[] array and simply delete the text. However, I only know how to delete the text with a String, not a String[]. Please take a look:

import java.util.ArrayList;
import java.util.List;
import jsc.independentsamples.SmirnovTest;

public class example {
    public static void main(String[] arg) throws Exception {
        ArrayList<String> list1 = new ArrayList<String>();
        ArrayList<String> list2 = new ArrayList<String>();

        list1.add("RSP0001,1.11,1.22");
        list1.add("RSP0002,2.11,2.22");
        list1.add("RSP0003,3.11,3.22");
        list1.add("RSP0004,4.11,4.22");

        String[] str1 = new String[list1.size()];
        str1 = list1.toArray(str1);
        str1.replaceAll("RSP_\\d+","");
        double array1 = Double.parseDouble(str1);

        System.out.println(array1);
    }
}

由此产生了两个错误:第一个是 str1.replaceAll 中的找不到符号"错误.第二个是Double.parseDouble"中的method parseDouble"错误.问题是我需要一个字符串而不是一个字符串 [].

Two errors come from this: the first is a "cannot find symbol" error at str1.replaceAll. The second is a "method parseDouble" error at "Double.parseDouble". The issue there is I need a String instead of a String[].

关于如何将我的 String[] 转换为 double[] 的任何想法?

Any ideas on how to convert my String[] to a double[] ?

谢谢,

kjm

推荐答案

您需要在 "," 上拆分 list1 中的每个 String 并尝试解析每个拆分出来的 String:

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();

关键点:

  • 编辑:正如@Tim Herold 指出的那样,您最好避免尝试解析您知道是非数字的内容,从而提高性能.在这种情况下,我仍然会先拆分,然后放入代码以防止您尝试在每行中的第一个拆分字符串上尝试 parseDouble(),而不是尝试在拆分之前进行字符串替换;那会更快(如果我们不关心性能,那么 try/catch 非常好且更具可读性).ORIGINAL:当您尝试解析双打时,您需要尝试/捕获,以防出现任何无效输入.奖励:你现在不需要删除非数字文本,你可以让这个 try/catch 处理它.
  • 您的字符串在每个字符串中有两个双打.您不能只在开头去除文本然后解析其余部分,因为它不会是有效的双精度数.
  • ArrayLists 通常更容易使用;我会选择返回 ArrayList(或 ArrayList>)而不是 Double[]Double[][] 一周中的任何一天.在某些情况下,我会采取不同的做法,但在我看来,您的情况并不像其中之一.
  • 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.

这篇关于将带有文本的 String[] 转换为 double[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆