Java - Why no out of bounds error for this array

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:
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   Reactions: 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
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K