CS205
Implement ArrayList add()Step 1 of 3
1
2
3
Validate the index

First, check if the index is valid. For add(), the valid range is 0 to size (inclusive). If invalid, throw an IndexOutOfBoundsException.

Loading editor...
Learning Objectives
  • Understand how ArrayList shifts elements on insert
  • Handle array resizing when capacity is reached
  • Implement boundary checking for valid indices