I've reviewed the "may be related" questions but they don't seem to help; I'm also a beginner, particularly with Java8, so please bear with me!
The problem - I want to read a file containing a list of values, in this case double numerics, into an array. I'm doing these as methods in a helper class as I expect to need to do this numerous times in my project.
I've already created a simple method to convert a file into a 2d double array -
public static double[][] Double2DMatrixFromFile(String input) throws IOException
{
Path path = Paths.get(input);
return Files.lines(path)
.map((x) -> Stream.of(x).mapToDouble(Double::parseDouble).toArray())
.toArray(double[][]::new);
}
This works, and I've been using it successfully. I then wanted to try reading a file containing a long list of big numbers into a 1D array so tried to quickly modify the method above to look like this -
public static double[] DoubleArrayFromFile(String input) throws IOException
{
Path path = Paths.get(input);
return Files.lines(path)
.map((y) -> Stream.of(y).mapToDouble(Double::parseDouble).toArray())
.toArray(double[]::new);
}
However this refuses to compile and it's not clear to me why. The exact error is
Error:(57, 17) java: method toArray in interface java.util.stream.Stream cannot be applied to given types;
required: java.util.function.IntFunction found: double[]::new
reason: inference variable A has incompatible bounds equality constraints: double upper bounds: java.lang.Object
I'm not clear where the incompatibility arises, given it works in the first case.
Apologies if this seems obvious, however I'm very new to coding.