I'm trying to make some test data with which to test some functionality of my code. To this end, I need a double[][]. I am trying to do with a function that takes a double[][] as an input parameter and copying onto it a local variable containing the test data. However, I get an error that I don't quite understand (I'm sure it's a very basic error, which is why I'm unable to Google it), understanding/fixing which I'd appreciate any help.
private void makeData(double[][] patterns)
{
double[][] data = new double[2][];
// exists so that I can change `data` easily, without having to change the core functionality of copying it over to `patterns`
data[0] = {1.0,8.0}; // error!
// copy over everything from data into patterns
}
The line marked in the above code is giving me the error Only assignment, call, increment, decrement, and new objects can be used as a statement. To this, my reaction is "Isn't data[0] = {1.0,8.0}; an assignment?
I'm fairly confused, so I'd appreciate any help
You want to do
data[0] = new[] {1.0, 8.0};
The curly brace initializers are only valid if you're creating an object/array. They don't work by themselves.
You can specify the type specifically:
data[0] = new double[] {1.0, 8.0};
But you don't have to if the compiler can infer the right type (which, in your case, it can).
Just replace:
data[0] = {1.0,8.0};
by:
data[0] = new double[] { 1.0, 8.0 };
The compiler has to know explicitly what to assign to data[0]. It doesn't infer it from the type of data[0].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With