我要做的首先在一个TXT文档里读取数据,可读出的是String类型(text里内容是我自己编写的,可以改变形式)
读出后String text={{0.49,-0.008,0.0057,0.5031,113.870,21.556},{0.498,-0.0053145,0.0029,0.4954,25.070,188.0549},{0.501,-0.0129,-0.008,0.49376,195.740,192.23}};
然后我想定义一个二维double型数组arr ,使上面的数值全部赋予这个数组
如 double arr[0][0]=0.49;
或者说是不是有更好的办法读出~~,我是新手~请大哥大姐赐教下~~~

解决方案 »

  1.   

    Double.parseDouble(String s)这个方法可以把String 转化成 double
      

  2.   

    通过迭代把字符串赋到数组里面,在赋值之前用上Double.parseDouble(String str)方法转换成double型。
      

  3.   

    把读出来的东西使用for循环,一个一个转化Double.parseDouble(String s) 
    然后再赋值给double数组就可以了
      

  4.   

    通常的思路应该都是读出字符串后再依次拆开来给数组赋值。如果改变一个格式,可能有利于降低拆分字符串的复杂度,那就是:0.49,-0.008,0.0057,0.5031,113.870,21.556
    0.498,-0.0053145,0.0029,0.4954,25.070,188.0549
    0.501,-0.0129,-0.008,0.49376,195.740,192.23-----------此外,你可以了解一下json,如果以json的格式存储,可以方便的在字符串和java对象之间进行互转。具体你可以到google上搜。
      

  5.   

    解析拆分字符串text的时候你也可以用正则表达式
    public class TestRegex
    {
    public static void main(String[] args)
    {
    String text="{{0.49,-0.008,0.0057,0.5031,113.870,21.556},{0.498,-0.0053145,0.0029,0.4954,25.070,188.0549},{0.501,-0.0129,-0.008,0.49376,195.740,192.23}}";
    String[] str1 = text.replace("{{", "").replace("}}", "").split("},{");
    String[] str2;
    double[][] d = new double[str1.length][];
    for (int i = 0; i < str1.length; i++)
    {
    str2 = str1[i].split(",");
    for (int j = 0; j < str2.length; j++)
    {
    d[i][j] = Double.parseDouble(str2[j]);
    }
    }
    }
    }
      

  6.   

    稍微该了一下,拆分的时候用到了正则表达式,要对大括号做一下处理
    public class TestRegex
    {
    public static void main(String[] args)
    {
    String text="{{0.49,-0.008,0.0057,0.5031,113.870,21.556},{0.498,-0.0053145,0.0029,0.4954,25.070,188.0549},{0.501,-0.0129,-0.008,0.49376,195.740,192.23}}";
    String[] str1 = text.replace("{{", "").replace("}}", "").split("[}],[{]");
    String[] str2;
    double[][] d = new double[str1.length][];
    for (int i = 0; i < str1.length; i++)
    {
    str2 = str1[i].split(",");
    d[i] = new double[str2.length];
    for (int j = 0; j < str2.length; j++)
    {
    d[i][j] = Double.parseDouble(str2[j]);
    }
    }
    }
    }
      

  7.   

    public class TestAar {
    public static void main(String[] args) {
    String text = "{ { 0.49, -0.008, 0.0057, 0.5031, 113.870, 21.556 },{ 0.498, -0.0053145, 0.0029, 0.4954, 25.070, 188.0549 },{ 0.501, -0.0129, -0.008, 0.49376, 195.740, 192.23 } }"; double[][] arr = new double[3][];
    String[] arr1 =  text.split("}");
    for(int i=0;i<arr1.length;i++){
    String[] arr2 = arr1[i].split("\\{");
    String string = arr2[arr2.length-1];
    if(string.length()>1){
    String[] arr3 = string.split(",");

    arr[i] = new double[arr3.length];

    for(int j=0; j<arr3.length;j++){
    arr[i][j] = Double.valueOf(arr3[j]);
    }
    }
    }

    for(int i=0;i<arr.length;i++){
    for(int j=0;j<arr[i].length;j++){
    System.out.print(arr[i][j] + " ");
    }
    System.out.println();
    }
    }
    }
      

  8.   

    解析字符串,规则要定好
    不知道LZ是只对应2维数组,还是任意维数组都要对应
    用堆栈应该比较准确,这里偷懒了,用正则表达式class Test {
        public static void main(String[] args) {
            try {
                test();
            } catch (Thrwable e) {
                e.printStackTrace();
            }
        }    static void test013() {
            String s = "{{{0.5, -0.67, 5.2},{1, 2, 3}},{{-1, -2, -3},{0, 0, 0}}}";
            List<?> list = analyzeExpress(s);
            printArray(list);
            
            System.out.println("------");
            s = "{{0.5, -0.67, 5.2},{1, 2, 3}}";
            list = analyzeExpress(s);
            printArray(list);
            System.out.println("-----2D Array Start-----");
            double[][] d = to2DArray(list);
            for (int i=0; i<d.length; i++) {
                for (int j=0; j<d[i].length; j++) {
                    System.out.printf("d[%d][%d]=%.2f ", i, j, d[i][j]);
                }
                System.out.println();
            }
            System.out.println("-----2D Array End-----");
            
            
            System.out.println("------");
            s = "{0.5, -0.67, 5.2}";
            list = analyzeExpress(s);
            printArray(list);
            
            System.out.println("------");
            s = "0.5, -0.67, 5.2";
            list = analyzeExpress(s);
            printArray(list);
        }
        
        static List<?> analyzeExpress(final String value) {
            int count = 0;
            for (int i=0; i<value.length(); i++) {
                if (value.charAt(i) == '{') {
                    count++;
                } else {
                    break;
                }
            }
            if (count == 0) {
                List<String> list = new ArrayList<String>();
                String[] va = value.split("[\\s[,][\\u007D]]");
                if (va != null && va.length > 0) {
                    for (String v : va) {
                        if (v.isEmpty() == false && v.matches("(-)??(\\d)+?(\\.\\d{1,})??")) {
                            list.add(v);
                        }
                    }
                }
                return list;
            } else if (count == 1) {
                String doubleStr = "(-)??(\\d)+?(\\.\\d{1,})??([\\s[,][\\u007D]])";
                Pattern p = Pattern.compile(doubleStr); 
                List<String> list = new ArrayList<String>();
                Matcher m = p.matcher(value);
                while (m.find()) {
                    String v = m.group();
                    v = v.replaceAll("[\\s[,][\\u007D]]", "");
                    if (v.isEmpty() == false) {
                        list.add(v);
                    }
                }
                return list;
            }
            
            List<Object> result = new ArrayList<Object>();
            String s1 = "";
            String s2 = "";
            for (int i=1; i<count; i++) {
                s1 += "\\u007B";
                s2 += "\\u007D";
            }
            String s = s1 + "[^\\u007B].*?[^\\u007D]" + s2;
            Pattern p = Pattern.compile(s);
            Matcher m = p.matcher(value);
            while (m.find()) {
                List<?> list = analyzeExpress(m.group());
                result.add(list);
            }
            return result;
        }
        
        static void printArray(final List<?> list) {
            System.out.println("size:" + list.size());
            for (Object o : list) {
                if (o instanceof String) {
                    System.out.printf("%s ", o);
                } else if (o instanceof List) {
                    printArray((List<?>)o);
                } else {
                    System.out.print("error");
                }
            }
            System.out.println();
        }
        
        //如果只对应2维数组
        static double[][] to2DArray(List<?> list) {
            double[][] d = new double[list.size()][0];
            int idx1=0, idx2=0;
            for (Object o : list) {
                if (o instanceof List) {
                    idx2 = 0;
                    d[idx1] = new double[((List<?>)o).size()];
                    for (Object o1 : (List<?>)o) {
                        if (o1 instanceof String 
                                && ((String)o1).matches("(-)??(\\d)+?(\\.\\d{1,})??")) {
                            d[idx1][idx2] = Double.valueOf((String)o1).doubleValue();
                        }
                        idx2++;
                    }
                }
                idx1++;
            }
            
            return d;
        }
    }
      

  9.   

    public class TestAar {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String text="{{0.49,-0.008,0.0057,0.5031,113.870,21.556},{0.498,-0.0053145,0.0029,0.4954,25.070,188.0549},{0.501,-0.0129,-0.008,0.49376,195.740,192.23}}";
    String[] str=text.replace("{{","").replace("}}","").split("[}],[{]");
    System.out.println(str.length);
    String[] str1=null;
    double[][] arr=new double[str.length][];
    for(int i=0;i<str.length;i++){
    str1=str[i].split(",");
    System.out.print("\n");
    for(int j=0;j<str1.length;j++){
    arr[i][j]=Double.parseDouble(str1[j]);
    System.out.print(str1[j]+"   ");
    }

    }
    }}报空指针异常,怎么搞的吗