Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

Java List<Integer>

I have the following method:

public static List<Integer> gradingStudents(List<Integer> grades) {
    for(int i = 0; i < grades.size(); i++){
        if(grades.get(i) < 40)
            continue;
        else if(grades.get(i) % 5 < 3)
            grades.get(i) = grades.get(i) + (Math.abs(grades.get(i)%5-5));     
        else
            continue;
    }
    return grades;
}

I get an unexpected type error at else if part. Why can't I change the element of grades, like I wrote in the code, above?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot assign to grades.get(i).

I suggest you simplify your code as follows:

public static List<Integer> gradingStudents(List<Integer> grades) {
    for(int i = 0; i < grades.size(); i++){
        int grade = grades.get(i);
        if (grade >= 40 && grade % 5 < 3) {
            grades.set(i, grade + Math.abs(grade%5-5));   
        }  
    }
    return grades;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...