Java I';我试图使用倒计时每秒将值放入数组,但它';它不工作了 公共类计算器{ float[]countPerSec=新浮点[600]; float[]finishedArray=新的float[600]; 浮动[]欧米伽雷=新浮动[600]; 浮动[]Darray=新浮动[600]; int半径=55; 浮标风速表射线[]=新浮标[600]; 浮动风速=0; float[]windspeedMinMax=新浮点[2]; int timerArrayCounter=0; int sensorCountValue=20;//**传感器每秒返回的计数** //此函数将以每秒0到599的速度更新x值 公共浮点[]获取PersecValues(){ 新的倒计时(60000,1000){ 公共void onTick(长毫秒未完成){ //while(timerArrayCounter==59){ Log.d(“计时器”、“运行时钟”); countPerSec[timerArrayCounter++]=sensorCountValue; 字符串abc=Float.toString(countPerSec[timerArrayCounter]); Log.d(“计时器”,abc); // } } 公共无效onFinish(){ 对于(int c=0;c

Java I';我试图使用倒计时每秒将值放入数组,但它';它不工作了 公共类计算器{ float[]countPerSec=新浮点[600]; float[]finishedArray=新的float[600]; 浮动[]欧米伽雷=新浮动[600]; 浮动[]Darray=新浮动[600]; int半径=55; 浮标风速表射线[]=新浮标[600]; 浮动风速=0; float[]windspeedMinMax=新浮点[2]; int timerArrayCounter=0; int sensorCountValue=20;//**传感器每秒返回的计数** //此函数将以每秒0到599的速度更新x值 公共浮点[]获取PersecValues(){ 新的倒计时(60000,1000){ 公共void onTick(长毫秒未完成){ //while(timerArrayCounter==59){ Log.d(“计时器”、“运行时钟”); countPerSec[timerArrayCounter++]=sensorCountValue; 字符串abc=Float.toString(countPerSec[timerArrayCounter]); Log.d(“计时器”,abc); // } } 公共无效onFinish(){ 对于(int c=0;c,java,android,timer,Java,Android,Timer,计时器功能运行,但并非每秒都将sensorCountValue的值20放入数组。我的错误是什么? 另一件事是,当我在没有计时器的情况下运行代码时,我得到ArrayOutOfBounds异常。再说一次,是什么错误? 感谢帮助!值正在放入数组中,问题是您在插入时正在推进索引,因此您的log语句实际上正在获取数组中的下一个条目: public class Calculator { float[] countPerSec = new float[600]; float[] finishedArray

计时器功能运行,但并非每秒都将
sensorCountValue
的值20放入数组。我的错误是什么? 另一件事是,当我在没有计时器的情况下运行代码时,我得到
ArrayOutOfBounds
异常。再说一次,是什么错误?
感谢帮助!

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

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;


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

             countPerSec[timerArrayCounter++] = sensorCountValue;

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