简体   繁体   English

将数据实时存储在Java中的2D数组中

[英]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. 此数据代表一个样本,我每秒读取1000个样本的数据,因此,如果我打印此数组,则表示每次读取的读数,是指每秒1000个数组,如图片(具有不同的数据)。

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. 现在,我需要处理这些数据,因此我需要将前256个样本存储在2D数组中,处理该数组,然后使用接下来的256个样本获得一个新的数组,依此类推。等等。但是我一直无法做这个。

transformed if my 1D array that shows me sample by sample. transformed ,如果我的一维数组,显示我逐个样本。 And buff is the matrix I want to store the data into. buff是我要将数据存储到的矩阵。

This is how I get transformed, it is first short[] and then I'm converting it to double: 这就是我的转换方式,首先是short [],然后将其转换为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. 但是我的buff数组具有相同的值。

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. 您要创建一个每次转换都要使用的新数组,然后将其加载到该数组中,因此所有行都是不同的数组,从而使它们具有不同的值。

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

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