Java - Why no out of bounds error for this array

Click For Summary
SUMMARY

The discussion centers on a Java class named "Course" that manages a list of student names. The user expected an out-of-bounds error when attempting to access an element in an empty array but discovered that the for loop does not execute due to the condition being false (i.e., students.length - 1 equals -1). This means the code correctly avoids the error, as the loop is never entered. The clarification provided by another user highlights the importance of understanding loop conditions in Java.

PREREQUISITES
  • Understanding of Java array initialization and manipulation
  • Familiarity with Java control structures, particularly loops
  • Knowledge of Java class and object concepts
  • Basic debugging skills in Java
NEXT STEPS
  • Review Java array handling and resizing techniques
  • Learn about Java loop constructs and their execution flow
  • Explore Java exception handling to manage out-of-bounds errors
  • Investigate best practices for managing dynamic arrays in Java
USEFUL FOR

Java developers, computer science students, and anyone interested in understanding array behavior and control flow in Java programming.

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   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
1K
  • · 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