0

I am looking for help with a portion of my program. I was wondering how I can loop through the entire "students" ArrayList before removing or keeping the "student" in the ArrayList. The updateStatus method parameter is an int and returns a boolean: true if the student has the required amount of credits to graduate, and false if they don't have enough. If you would like me to post my code for the updateStatus (which I know work 100% accurately), I can do so.

Here is the for-loop I am having an issue with:

ArrayList<Student> students = new ArrayList<Student>();        
int [] updates = {4, 7, 8, 6, 15, 10, 30, 31, 5, 7, 127, 21}; //this is the array of values being input to updateStatus

for (int i = 0; i < students.size(); i++){
    for(Student x: students){
        if(x.updateStatus(updates[i])){
            System.out.println(students.remove(i)+"\nCongratulations Graduate!");
        }
    }
}

Thank you so much in advance!

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
pyr0
  • 15
  • 3
  • As you look through the list, use another List to add the students you want removed to. You could split the list into two lists (keep/remove) or simply remove the “to remove” items directly from the original list – MadProgrammer Jan 31 '18 at 01:49
  • Possible cross site duplicate: https://codereview.stackexchange.com/questions/64011/removing-elements-on-a-list-while-iterating-through-it – Gaurang Tandon Jan 31 '18 at 01:51
  • Is your double for loop intentional? Do you really want to call updateStatus 12 times for each student, passing the same set of 12 data items to each student? – AJNeufeld Jan 31 '18 at 01:52
  • The relationship between `updates` and `updateStatus` is unclear. Please elaborate on what you're trying to do. – shmosel Jan 31 '18 at 02:03
  • Possible duplicate of [Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop](https://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) – AJNeufeld Jan 31 '18 at 02:23

1 Answers1

2

It seems your array of ints contains all the credits each student has. Student x has 4 credits, next student has 7 credits, etc. If that is true, calling it updates makes absolutely no sense. Name your variables more appropriately, otherwise, this example is sure hard to follow. Why not associate the credits with the Student object and while looping through you call a method like getCredits() to check their credits. In addition, inside your student object you could have a field isGraduate that's a boolean and says whether the student qualifies for graduation or not. This value is checked everytime a student earns credits.. so it checks periodically.

This code is very confusing, you're looping the number of students while looping the number of students... You can't do that.

Also, it seems you are calling a method name .remove inside your output. No idea what this method does but I don't see a case where this should be placed inside a System.out.println... if the method removes the student from the list, this is going to severally mess up your loop and cause problems.

It's not clear what your goal is and if you're not sure what you're trying to achieve no one here is going to be able to help you, unfortunately.

kimcodes
  • 736
  • 1
  • 6
  • 16