elegantly working with multiple arrays
in
Programming Questions
•
1 year ago
Hi all,
I'm working on a script that has to store and later sort two values from 5 sensors. So that would be 10 in all. For sorting the arrays I'll need temporary arrays so that will add up to quite a few. I want to avoid hardcoding all the arrays and having to go through them using a switch statement, that seems a bit silly, there must be a more elegant way.
My idea was to make two 2D arrays:
float[][] windCalibration = {};
float[][] tempCalibration = {};
The first value would be the sensor number (1 to 5), the second the float value of the sensor output.
I want to loop through the sensors and fill sensor 0 with value xx, sensor 1 with value yy, etc.
I've tried this using append, as the length of the arrays may vary. Here's some code I found on the forum:
for(int j = 0; j<5; j++){
windCalibration = (float[][])append(windCalibration, new float[]{j, wind0});
}
When I print windCalibration[0][0] nothing happens.
I also tried:
for(int j = 0; j<5; j++){
windCalibration[j] = append(windCalibration[j], wind0);
}
I get errors elsewhere in the program.
I realise that I'm also using ints and floats at the same time in my array but I can think of no other way to do this.
Can some of you gurus point me in the right direction? Thanks very much in advance for your help, best, d
I'm working on a script that has to store and later sort two values from 5 sensors. So that would be 10 in all. For sorting the arrays I'll need temporary arrays so that will add up to quite a few. I want to avoid hardcoding all the arrays and having to go through them using a switch statement, that seems a bit silly, there must be a more elegant way.
My idea was to make two 2D arrays:
float[][] windCalibration = {};
float[][] tempCalibration = {};
The first value would be the sensor number (1 to 5), the second the float value of the sensor output.
I want to loop through the sensors and fill sensor 0 with value xx, sensor 1 with value yy, etc.
I've tried this using append, as the length of the arrays may vary. Here's some code I found on the forum:
for(int j = 0; j<5; j++){
windCalibration = (float[][])append(windCalibration, new float[]{j, wind0});
}
When I print windCalibration[0][0] nothing happens.
I also tried:
for(int j = 0; j<5; j++){
windCalibration[j] = append(windCalibration[j], wind0);
}
I get errors elsewhere in the program.
I realise that I'm also using ints and floats at the same time in my array but I can think of no other way to do this.
Can some of you gurus point me in the right direction? Thanks very much in advance for your help, best, d
1