简体   繁体   中英

Store data in real time in a 2D array in Java

I'm having trouble storing real time data in an array.

I have a 1D array that reads data in real time, this is fine and it's printing me data like this:

D/this is my array: arr: = [-1.43, -3.5916, 2.71, 4.42, -4.4, 0.0]

This data represents ONE sample, and I'm reading data 1000 samples per second, so if I print this array, it shows me reading per reading, I mean 1000 arrays like the picture (with different data) per second.

Now, I need to process that data, so I need to store the first 256 samples in a 2D array, process that array and then get a new one with the next 256 samples and so on.. But I haven't been able to do this.

transformed if my 1D array that shows me sample by sample. And buff is the matrix I want to store the data into.

This is how I get transformed, it is first short[] and then I'm converting it to double:

short[] yaxis = msg.getData().getShortArray(BiopluxService.KEY_FRAME_DATA);
double[] transformed = new double[yaxis.length];
for (int j = 0; j < yaxis.length; j++) {
    transformed[j] = (double) yaxis[j];
}

This is what I have so far:

double[][] buff = new double[256][6];
for (int f = 0; f < 256; f++) {
    buff[f] = transformed;
}
Log.d("this is my array", "arr: " + Arrays.deepToString(buff));

But my buff array has the same values.

Why doesn't the array contain different values

You are setting all the rows to point to the same array, if you run this code:

public class ClassNameHere {
   public static void main(String[] args) {
      double[][] d = new double[5][];

      double[] row = {1,2,3};
      for (int i = 0; i < 5; i++) {
         d[i] = row;
      }
   }
}

Through this tool, you can see that at the end of this code all of the rows point to the exact same array, so they will all be equal.

How to fix

You want to create a new array for transformed each time, and then load into that array, so all the rows are different arrays, allowing them to have different values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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