From 825f1f71d80fd82b4142983bae1125fd28bc3e97 Mon Sep 17 00:00:00 2001 From: xiaolinli123 <154345419+xiaolinli123@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:58:20 +0800 Subject: [PATCH] Update Course.java Charpter_10/Problem$09, The code in the original answer might result in an array index out of bounds exception during execution. The changes I submitted have resolved this issue. --- Chapter_10/Problem$09/Course.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Chapter_10/Problem$09/Course.java b/Chapter_10/Problem$09/Course.java index 0d47e50..efc3e9a 100644 --- a/Chapter_10/Problem$09/Course.java +++ b/Chapter_10/Problem$09/Course.java @@ -50,16 +50,17 @@ public void clear() } - public void dropStudent(String studentName) + public void dropStudent(String studentName) { boolean reach = false; - for (int i = 0; i < numberOfStudents+1;i++) - { + for (int i = 0; i <= numberOfStudents-1;i++) { if (!reach) if (students[i].equals(studentName)) reach = true; + if (reach) - students[i] = students[i+1]; + if (i < numberOfStudents - 1) + students[i] = students[i + 1]; } numberOfStudents--; }