TheAlgorithms/Java · error · IllegalArgumentException
Capacity cannot be negative.
Error message
Capacity cannot be negative.
What it means
The DynamicArray constructor rejects negative capacity because the backing Object[] array cannot have a negative length. Java would throw an obscure NegativeArraySizeException otherwise; this guard provides a clearer, catchable IllegalArgumentException. A capacity of 0 is permitted (creates an empty backing array that grows on demand).
Source
Thrown at src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java:35
*
* @param <E> the type of elements that this array can hold
*/
public class DynamicArray<E> implements Iterable<E> {
private static final int DEFAULT_CAPACITY = 16;
private int size;
private int modCount; // Tracks structural modifications for iterator integrity
private Object[] elements;
/**
* Constructs a new DynamicArray with the specified initial capacity.
*
* @param capacity the initial capacity of the array
* @throws IllegalArgumentException if the specified capacity is negative
*/
public DynamicArray(final int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("Capacity cannot be negative.");
}
this.size = 0;
this.modCount = 0;
this.elements = new Object[capacity];
}
/**
* Constructs a new DynamicArray with a default initial capacity.
*/
public DynamicArray() {
this(DEFAULT_CAPACITY);
}
/**
* Adds an element to the end of the array. If the array is full, it
* creates a new array with double the size to accommodate the new element.
*
* @param element the element to be added to the arrayView on GitHub (pinned to fdfb9a395b)
Solutions
- Validate that capacity >= 0 before constructing
- Use the no-arg constructor (defaults to capacity 16) when you do not have a specific size requirement
- Clamp to 0 if negative capacity is possible from the computation
Example fix
// before DynamicArray<String> arr = new DynamicArray<>(requestedSize - buffer); // after int cap = Math.max(0, requestedSize - buffer); DynamicArray<String> arr = new DynamicArray<>(cap);
Defensive patterns
Strategy: validation
Validate before calling
int cap = Math.max(0, requestedCapacity); DynamicArray<String> arr = new DynamicArray<>(cap);
Try / catch
try {
arr = new DynamicArray<>(capacity);
} catch (IllegalArgumentException e) {
arr = new DynamicArray<>(); // default capacity 16
} Prevention
- Validate capacity >= 0 before construction
- Use the no-arg constructor for the default capacity of 16
- Clamp computed sizes with Math.max(0, value)
When it happens
Trigger: Calling new DynamicArray<>(-1) or passing any negative int to the constructor. Can also arise from arithmetic that produces a negative result.
Common situations: Capacity derived from a subtraction or a computed size that goes negative on edge cases. Config values or user input passed through without validation.
Related errors
- capacity must greater than 0!
- Capacity must be greater than 0!
- Index cannot be negative.
- initialCapacity < 1
- Denominator cannot be 0
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/b2099ba301cccb5a.
Report an issue: GitHub.