java:将数据保存到数组中

mel*_*and 0 java arrays

我正在尝试将一些数据保存到数组中但不幸的是所有数据都保存到数组[0]中,而它通常不应该这样.

int j = 0;
while ( j < data.length ) { 
    float unbin = binary(data[2*j+1])+binary(data[2*j]);
    float[] bin = new float[] {unbin};
    print(bin);
    j = j + 2;
}
Run Code Online (Sandbox Code Playgroud)

它写了所有的数据bin[0],我的代码有什么问题?

我该怎么写:

bin[j] = unbin ? 
Run Code Online (Sandbox Code Playgroud)

在j = 0时将数据保存在bin [0]中,依此类推?

这是更新的代码:

    int j = 0 ;
  float[] bin1 = new float[(data.length/2)];
  while (j < data.length ) {
    if ( data[2*j+2] >= 0  ) {

      String unhx =(binary(data[2*j+3])+binary(data[2*j+2])+binary(data[2*j+1])+binary(data[2*j]));
      float unbin = ((float)unbinary(unhx)/100);
      bin1[j/2] = unbin;
      print(bin1[1]);
    }

    else if  ( data[2*j+2] < 0 && data[2*j+3] < 0 ) {
      data[2*j] = (byte)(-data[2*j]);
      data[2*j+1] = (byte)(-data[2*j+1]);
      String unhx =(binary(data[2*j+1])+binary(data[2*j]));
      float unbin = ((-1)*(float)unbinary(unhx)/100);
      bin1[j/2] = unbin;
      print(bin1[1]);
      }
      j = j + 2;
  }
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 7

您在每次迭代时创建一个数组(长度为1).您需要在while循环之前创建数组:

float[] bin = new float[...];
while (...) {
    ...
    bin[j] = unbin;
}
Run Code Online (Sandbox Code Playgroud)

(目前还不清楚长度应该在这里 - 我怀疑你不想乘以j2 并且每次增加2.)