Ben aşağıda sunulmuştur printStat()
yöntemi içinde, bir ArrayList
elemanların üzerinde minimum ve maksimum belirlemek için benim StudentScore
sınıfında getScore()
yöntemi çağırmak çalışıyorum . Bir ArrayIndexOutOfBoundException
alıyorum. Bu ne anlama geliyor ve sorunu nasıl çözebilirim?bir arraylist unsurları karşılaştırmak olamaz, nesnenin Arraylist içinde min bulun
public class ScoreCalculator{
private int[] scoreCounter;
ArrayList<StudentScore> scores ;
public ScoreCalculator(int maxScore) {
scoreCounter = new int[maxScore];
scores = new ArrayList<StudentScore>(maxScore);
}
public void printStat() {
System.out.println("Score Report for " + scores.size() + " Students ");
min = 0;
max = 0;
int j=0;
for (j = 0; j < scores.size(); j++) {
if (scores.get(j).getScore() < scores.get(j - 1).getScore()) {
min = scores.get(j).getScore();
}
if (scores.get(j).getScore() > scores.get(j - 1).getScore()) {
max = scores.get(j).getScore();
}
}
System.out.println(min);
System.out.println(max);
}
'j-1' Kodunuzdaki negatif olabilir, endeks aralığı boyut() -1 ile 0, negatif değer izin verilmez – haifzhan
Sen eleman' j-1' de erişir. Listenizi sipariş etmenizi öneririm, böylece ilk öğe "min" ve sonuncudur "max". –