I have a dto class which stores some studentid and marks of particular subject. basically like this.
List<StudentInfoDTO> studentInfoDTO = new ArrayList<>();
where StudentInfoDTO is like below
public class StudentInfoDTO {
Long studentId;
Short marks;
}
Now I want the student id who has smallest marks.
I tried below but not giving expected result.
int smallest = 0;
for(int i = 0; i < studentInfoDTO.size(); i++) {
smallest = studentInfoDTO.get(i).getMarks();
int x = studentInfoDTO.get(i).getMarks();
if (x < smallest) {
smallest = x;
}
}
smallestvariable to the element you're currently processing. Remove that first line of the loop, and initializesmallestto a very large number, then it ought to work.smalleststudent because if not later you won't be able to access it from from marks