A razão por trás de Matrizes de Inteiros
-
20-12-2019 - |
Pergunta
Eu achei esse código na internet (porque eu estou tentando criar um temporizador (timer).Alguém pode me dizer por que esses códigos não lançar uma IndexOutOfBoundsException
.
Aqui está o código:
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;
}
em seguida, é apresentado com um temporizador de tarefas
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);
Por que não lançar um IndexOutOfBoundsException
.Estou confuso porque ele é instanciado com um valor de 30 e quando eu tento executar este o runningTime exceeeds 30 100 em milissegundos, 59 sec assim por diante e assim por diante.
E aqui está o código completo:
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();
}
}
}
Solução
A razão é procura como não é accionada uma excepção é porque o código é terrível e captura a exceção de:
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
Alterar o bloco catch a este:
}catch(Exception e) {
e.printStackTrace();
}
E você vai ver que uma exceção é lançada.Em seguida, você pode:
- A) Retire o try/catch e alterar
<= 30
para< 30
. - B) Descartar o código, porque é um mau exemplo.
Como para o bloco em run
, e , tanto quanto eu posso dizer, contar nunca é alterado por isso é sempre 0.
Outras dicas
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;
}
Este código vai através de um IndexOutOfBounds exceção.A última passagem pelo loop j é de 30, o índice mais alto em qualquer matriz é 29.
Se ele não está lançando uma exceção, em seguida, este código não está sendo executado ou a exceção é ser apanhado.