Java - Why no out of bounds error for this array

  • Comp Sci
  • Thread starter JonnyG
  • Start date
  • #1
JonnyG
227
22
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:

Answers and Replies

  • #2
Filip Larsen
Gold Member
1,650
568
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.
 
  • #3
JonnyG
227
22
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!
 

Suggested for: Java - Why no out of bounds error for this array

Replies
5
Views
392
  • Last Post
Replies
3
Views
392
  • Last Post
Replies
2
Views
304
Comp Sci Truncation Error
  • Last Post
Replies
6
Views
648
Replies
1
Views
523
  • Last Post
Replies
12
Views
809
  • Last Post
Replies
7
Views
852
Replies
17
Views
718
  • Last Post
Replies
21
Views
1K
Top