整数数组背后的原因

时间:2014-02-24 23:02:34

标签: java arrays indexoutofboundsexception

我在互联网上找到了这个代码(因为我正在尝试创建一个计时器)。有人可以告诉我为什么这些代码不会抛出IndexOutOfBoundsException

以下是代码:

hour = new int[30];
min = new int[30];
sec = new int[30];
msec = new int[30];
start = false;
stop = true;
for(int j = 0  ; j <= 30 ; j++)
{
    hour[j] = 0;
    min[j] = 0;
    sec[j] = 0;
    msec[j] = 0;
}

然后使用计时器任务

进行渲染
    public void run()
    {
        msec[count]++;
        if(msec[count] == 100)
        {
            msec[count] = 0 ;
            sec[count]++;
        }
        else if(sec[count] ==60)
        {
            sec[count] = 0;
            min[count]++;
        }
        else if(min[count] == 60)
        {
            min[count] = 0;
            hour[count]++;
        }
        else if(hour[count] == 24)
        {
            hour[count] = 0;
        } 
        repaint();
    } 
};
timer = new Timer();
timer.scheduleAtFixedRate(task,10,67);    

为什么不抛出IndexOutOfBoundsException。我很困惑,因为它实例化的值为30,当我尝试运行它时,runningTime超过30毫秒,如毫秒,59秒,等等。

这是完整的代码:

 public class TimerCan extends Canvas
{
private Timer timer;
private Midlet myMid;
private Player z;
private int habaNgString,hour[],sec[],min[],msec[],maxX,maxY,count,length,x,y;
private String runningTime;
private boolean start,stop;
public Image img;
public TimerCan(Midlet midlet)
{
    this.myMid= midlet;
    try
    {
        maxX = getWidth();
        maxY = getHeight();
        count = 0;
        hour = new int[30];
        min = new int[30];
        sec = new int[30];
        msec = new int[30];
        start = false;
        stop = true;
        for(int j = 0  ; j <= 30 ; j++)
        {
            hour[j] = 0;
            min[j] = 0;
            sec[j] = 0;
            msec[j] = 0;
        }
    }catch(Exception e)
    {}
}
public void paint(Graphics g)
{
         if(hour[count] < 10)
        {
            runningTime = "0"+String.valueOf(hour[count])+":";
        }
        else
        {
            runningTime = String.valueOf(hour[count]) + ":";  
        }
        if(min[count] < 10)
        {
            runningTime = runningTime+"0"+String.valueOf(min[count]) + ":";
        }
        else
        {
            runningTime = runningTime+String.valueOf(min[count]) + ":";
        }
        if(sec[count] < 10)
        {
            runningTime = runningTime+"0"+String.valueOf(sec[count]) + ":";
        }
        else
        {
            runningTime = runningTime + String.valueOf(sec[count]) + ":";
        }
        if(msec[count] < 10)
        {
            runningTime = runningTime+"0"+String.valueOf(msec[count]);
        }
        else
        {
            runningTime = runningTime+String.valueOf(msec[count]);
        }

    try{
         img = Image.createImage("/picture/aa.png");
    }
    catch(Exception error){
    }
    x = getWidth()/2;
    y = getHeight()/2;
    g.setColor(63,155,191);
    g.fillRect(0,0,maxX, maxY);
    g.drawImage(img, x, y, Graphics.VCENTER|Graphics.HCENTER);
    g.setColor(0,0,0) ;                                               
    g.drawString(runningTime,maxX,maxY,Graphics.TOP|Graphics.LEFT);
}
private void startTimer()
{
    TimerTask task = new TimerTask()
    {
        public void run()
        {
           msec[count]++;
            if(msec[count] == 100)
            {
                msec[count] = 0 ;
                sec[count]++;
            }
            else if(sec[count] ==60)
            {

                sec[count] = 0;
                min[count]++;
            }
            else if(min[count] == 60)
            {
                min[count] = 0;

               hour[count]++;
            }
            else if(hour[count] == 24)
            {
                hour[count] = 0;
            } 
                   repaint();
        } 
    };
    timer = new Timer();
    timer.scheduleAtFixedRate(task,10,67);           
}
protected  void keyPressed(int keyCode)
{
    if(keyCode == Canvas.KEY_NUM1)
    {
        if(start == false)
        {
            start=true;
            stop=false;
        }
        else if(stop == false)
        {
            start = false ;
            stop = true ;
            timer.cancel();
        }
        if(start==true)
        {
            startTimer();
        }
    }
    if(keyCode == Canvas.KEY_NUM2)
    {                 
        min[count]=0;
        sec[count]=0;
        msec[count]=0;       
        start = false;
        stop = true;
        timer.cancel();
        try{
        z.deallocate();
        }
        catch(Exception e){}
        repaint();
    }
    if(keyCode == Canvas.KEY_NUM3)
        {
            if(stop == false)
            {
            start = false;
            stop = true;
            timer.cancel();
            try{
                InputStream inss = getClass().getResourceAsStream("alarm.wav");
                InputStreamReader iis= new InputStreamReader(inss);  
                z = Manager.createPlayer(inss,"audio/x-wav");
                z.prefetch();
                z.setLoopCount(2);
                z.start();
                }
    catch(Exception e){
    }
            }
        }
    if(keyCode==Canvas.KEY_NUM0)
    {
        try{
        z.deallocate();
        }
        catch(Exception e){}
        myMid.exit();
    }
 }
}

2 个答案:

答案 0 :(得分:4)

看起来像这样的异常的原因并不是因为代码很糟糕并且捕获了异常:

try
{
    maxX = getWidth();
    maxY = getHeight();
    count = 0;
    hour = new int[30];
    min = new int[30];
    sec = new int[30];
    msec = new int[30];
    start = false;
    stop = true;
    for(int j = 0  ; j <= 30 ; j++)
    {
        hour[j] = 0;
        min[j] = 0;
        sec[j] = 0;
        msec[j] = 0;
    }
}catch(Exception e)
{} // <- catches the exception and does nothing

将catch块更改为:

}catch(Exception e) {
    e.printStackTrace();
}

你会看到抛出异常。然后你可以:

  • A)删除try / catch并将<= 30更改为< 30
  • B)丢弃代码,因为这是一个不好的例子。

至于run中的块,据我所知,count永远不会改变,所以它总是为0。

答案 1 :(得分:0)

    hour = new int[30];
    min = new int[30];
    sec = new int[30];
    msec = new int[30];
    start = false;
    stop = true;
    for(int j = 0  ; j <= 30 ; j++)
    {
        hour[j] = 0;
        min[j] = 0;
        sec[j] = 0;
        msec[j] = 0;
    }

此代码通过IndexOutOfBounds异常。循环j的最终传递是30,任何数组中的最高索引是29。

如果没有抛出异常,则不会执行此代码或捕获异常。