2014-02-24 40 views
0

我在互聯網上發現了這個代碼(因爲我試圖創建一個計時器)。有人可以告訴我爲什麼這個代碼不會拋出IndexOutOfBoundsExceptionInteger背後的原因

下面的代碼:

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,如100毫秒,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(); 
    } 
} 
} 
+1

當然的第一個片段的確拋出異常。至於其他方面,我們不知道其他代碼是什麼。 (什麼是計數?) – Radiodef

+1

'for(int j = 0; j <= 30; j ++)'應該變成'for(int j = 0; j <30; j ++)',因爲在數組中可以有30個元素,索引是從0到29 – Athanor

+0

如果你的目的是學習編寫好的Java代碼,我建議把這個例子扔掉。如果你想學習如何修復不好的代碼,這可能會很有用。 –

回答

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塊,據我所知,計數從未改變,使得它一直爲0

+0

那麼如果count總是零?爲什麼我在運行這些代碼時可以看到增加的數字? – user3322668

+0

它們正在遞增數組中的int元素。 'someArr [someInt] ++' – Radiodef

+0

如果是這種情況,這些代碼中的for循環的目的是什麼? – user3322668

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.

如果它沒有引發異常,那麼此代碼未被執行或異常被捕獲。

+0

我添加了完整的代碼,以便您可以檢查它。 – user3322668

相關問題