Comp Sci Java - Why no out of bounds error for this array

Click For Summary
The discussion revolves around a Java class called "Course" where the user is confused about why an out-of-bounds error does not occur when adding a student to an empty array. The code initializes an array of length 0, yet the loop intended to copy existing students does not execute because the condition for the loop is never met. Specifically, since the length of the students array is 0, the loop's limit condition results in no iterations. The user realizes that the loop does not run at all, which is why no error is thrown. This clarification resolves the initial confusion regarding the behavior of the code.
JonnyG
Messages
233
Reaction score
45
Homework Statement
Create method to add students to course array.
Relevant Equations
no equations
I am writing a class called "Course" in Java so that one can input students names, etc. Here is my relevant code for the Course class:

Java:
public class Course {

    private String courseName;

    private String[] students = new String[0];

    private int numberOfStudents = 0;
    public Course(String courseName) {

        this.courseName = courseName;

    }
    public void addStudent(String student) {

        String[] studentsRevised = new String[students.length + 1];

  

        for (int i = 0; i <= students.length - 1; i++)

            studentsRevised[i] = students[i];

    

        studentsRevised[studentsRevised.length - 1] = student;

        numberOfStudents++;

        students = studentsRevised;

    }
As a little test, I wrote in the main method:

Java:
    Course course = new Course("math");

        course.addStudent("jonathan");

    

        for (String student : course.getStudents())

            System.out.println(student);

The output was "jonathan", but I was expecting an error! Let me explain:

The array "students" is initialized to be of length 0. In the Course class method addStudent, look at the line
Java:
 studentsRevised[i] = students[i].
When i is initially equal to 0 and the line
Java:
 studentsRevised[i] = students[i].
is executed, there should be an out of bounds error because students[0] doesn't exist. Why do I not get an error?
 
Last edited:
Physics news on Phys.org
I assume you did not write the code yourself and now want to understand why it works.

The single code line you quote (line 27 in your initial snippet, which by the way has omissions and does not match your later quote) is inside a loop. Try analyze how this loop is executed, i.e. what value variable i can take. Pay special attention to the limit condition.
 
  • Like
Likes JonnyG
Filip Larsen said:
I assume you did not write the code yourself and now want to understand why it works.

The single code line you quote (line 27 in your initial snippet, which by the way has omissions and does not match your later quote) is inside a loop. Try analyze how this loop is executed, i.e. what value variable i can take. Pay special attention to the limit condition.

I wrote it myself, but became confused afterward upon closer inspection of the code. But your hint was very helpful. I now see that the for loop in line 25 will never actually execute because I initially set i = 0, but the loop is only ran if i <= -1 (since students.length - 1 = -1).

Thanks!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K