gpt4 book ai didi

java - 我试图使用 CountDownTimer 每秒将值放入数组中,但它不起作用

转载 作者:行者123 更新时间:2023-12-01 12:38:39 25 4
gpt4 key购买 nike

public class Calculator {

float[] countPerSec = new float[600];
float[] finishedArray = new float[600];
float[] omegaArray = new float[600];
float[] sortedArray = new float[600];
int radius = 55;
float windSpeedArray[] = new float[600];
float windspeedAvg = 0;
float[] windspeedMinMax = new float[2];
int timerArrayCounter = 0;



int sensorCountValue = 20; //**count per second returned by the sensor**




//this function will update x values from 0 to 599 per second
public float[] fetchPerSecValues(){


new CountDownTimer(60000, 1000) {

public void onTick(long millisUntilFinished) {


// while(timerArrayCounter == 59){
Log.d("Timer","Running Clock");
countPerSec[timerArrayCounter++] = sensorCountValue;

String abc = Float.toString(countPerSec[timerArrayCounter]);
Log.d("Timer", abc);
// }


}

public void onFinish() {

for(int c = 0; c < 60; c++){
finishedArray[c] = countPerSec[c];
omegaArray[c] = (float) (finishedArray[c]*2*3.14);
windSpeedArray[c] = radius*omegaArray[c];

}


Log.d("Here","Finished Clock");
timerArrayCounter = 0;
fetchPerSecValues();




}
}.start();
return windSpeedArray;


}

计时器函数运行,但 sensorCountValue 的值(20)不会每秒放入数组中。我在这里犯了什么错误?另一件事是,当我在没有计时器的情况下运行代码时,我收到 ArrayOutOfBounds 异常。话又说回来,这又是什么错误呢?感谢帮助!

最佳答案

该值正在被放入数组中,问题是您在插入时正在推进索引,因此您的日志语句实际上正在获取数组中的下一个条目:

             countPerSec[timerArrayCounter++] = sensorCountValue;

String abc = Float.toString(countPerSec[timerArrayCounter]);
Log.d("Timer", abc);

仔细查看 logcat 输出中的堆栈跟踪,它会直接指向数组越界异常。

关于java - 我试图使用 CountDownTimer 每秒将值放入数组中,但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25334661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com