简体   繁体   English

将2D String数组转换为2D double

[英]Convert 2D String array to 2D double

I'm a bit confused right now. 我现在有点困惑。 I made this function to rewrite my 2D String array into a 2D double array. 我做了这个函数,将我的2D String数组重写为2D双数组。 But it's casting nullpointer exceptions. 但它正在抛出nullpointer异常。 The data in d2 is in the format : String[i][0/1] where 0 and 1 is a number in the format "0.3343434". d2中的数据格式为:String [i] [0/1]其中0和1是格式为“0.3343434”的数字。

  public void StringToDouble () {
     unsorted = new double[d2.length][2];

     for(int i = 0; i < d2.length; i++)
    {
        unsorted[i][0] = Double.parseDouble(d2[i][0]);    
        unsorted[i][1] = Double.parseDouble(d2[i][1]);
    }

}

I'm getting an error when i print unsorted[0][0] and [1][1]. 当我打印未排序的[0] [0]和[1] [1]时,我收到错误。

Here is the whole code. 这是整个代码。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Scanner;


public class LoadAndSort {

String raw;
double [][] data;
String [] datastring;

// 2D CONVERT

String [] d1;
String [][] d2;
double[][] unsorted;
double [][] sorted;
String path = "src/data.dat";


 public LoadAndSort () throws IOException {



    // # Reads file from disk and stores to variable.
    datas = readFile(path);
    // # Splits the content, and sorts it into a huge string with relevant variables.

    // # Splits on newline to order the lines.
    d1 = datas.split("\\r?\\n");
    // # Splits on comma to convert to 2D Array. 
    Convert2D();
    // # Mirrors the 2D array because Mikal is gay.
    //MirrorArray();
    //StringToDouble();

    System.out.println(unsorted[0][0]);
    System.out.println(unsorted[0][1]);

}



private static String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */

        return Charset.defaultCharset().decode(bb).toString();
    }
    finally {
        stream.close();
    }
}

public void Convert2D () {

    d2 = new String[d1.length][]; 
    int r = 0;
    for (String row : d1) {
        d2[r++] = row.split(",");
    }
}

public void StringToDouble () {

    unsorted = new double[d2.length][2];


    for(int i = 0; i < d2.length; i++)
    {
        unsorted[i][0] = Double.parseDouble(d2[i][0]);
        System.out.println(d2[i][0]);

        unsorted[i][1] = Double.parseDouble(d2[i][1]);
    }

}


}

And the error from console : 来自控制台的错误:

 0.00965821033009
 Exception in thread "main" java.lang.NullPointerException
at LoadAndSort.<init>(LoadAndSort.java:48)
at Runner.main(Runner.java:13)

You initialize unsorted in StringToDouble (with unsorted = new double[d2.length][2]; ), but the function call in the constructor is currently commented out, so unsorted will never be initialized. 你在StringToDouble初始化unsorted (使用unsorted = new double[d2.length][2]; ),但是构造函数中的函数调用当前已被注释掉,因此永远不会初始化unsorted

Thus it will be null and a NullPointerException will be thrown here: 因此它将为null并且将抛出NullPointerException

System.out.println(unsorted[0][0]);

Not sure whether this is the only problem, but first things first. 不确定这是否是唯一的问题,但首先要做的事情。

You can also use Streams API from Java 8 您还可以使用Java 8中的Streams API

Double[][] c = Arrays.stream(stringArray).map(
                a -> Arrays.stream(a).map(
                        b -> Double.valueOf(b)
                        ).toArray(Double[]::new)
                ).toArray(Double[][]::new);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM