TheAlgorithms/Java · error · IllegalArgumentException
initialCapacity < 1
Error message
initialCapacity < 1
What it means
Thrown by the IndexedPriorityQueue(int initialCapacity, Comparator) constructor when initialCapacity is less than 1. The constructor immediately allocates `new Object[initialCapacity]`, so a zero or negative value would yield an unusable or malformed backing array. The no-arg and Comparator-only constructors default to 11 to avoid this.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/heaps/IndexedPriorityQueue.java:77
* </ul>
* If you prefer value-based semantics, replace with HashMap<E,Integer> and
* respect the warnings in the class Javadoc.
*/
private final IdentityHashMap<E, Integer> index;
private static final int DEFAULT_INITIAL_CAPACITY = 11;
public IndexedPriorityQueue() {
this(DEFAULT_INITIAL_CAPACITY, null);
}
public IndexedPriorityQueue(Comparator<? super E> cmp) {
this(DEFAULT_INITIAL_CAPACITY, cmp);
}
public IndexedPriorityQueue(int initialCapacity, Comparator<? super E> cmp) {
if (initialCapacity < 1) {
throw new IllegalArgumentException("initialCapacity < 1");
}
this.heap = new Object[initialCapacity];
this.cmp = cmp;
this.index = new IdentityHashMap<>();
}
/** Returns current number of elements. */
public int size() {
return size;
}
/** Returns {@code true} if empty. */
public boolean isEmpty() {
return size == 0;
}
/**
* Returns the minimum element without removing it, or {@code null} if empty.View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass a value >= 1, or use the no-arg / Comparator-only constructor to get the default capacity of 11.
- Clamp externally sourced capacity: `Math.max(1, configuredCapacity)` before constructing.
- Validate configuration at load time and fail fast with a clear config error rather than at PQ construction.
Example fix
// before new IndexedPriorityQueue<>(config.getCapacity(), cmp); // after int cap = Math.max(1, config.getCapacity()); new IndexedPriorityQueue<>(cap, cmp);
Defensive patterns
Strategy: validation
Validate before calling
int cap = Math.max(1, configuredCapacity); new IndexedPriorityQueue<>(cap, cmp);
Prevention
- Default capacity reads from config to 11 (or another positive value), never 0.
- Prefer the no-arg or Comparator-only constructor when default capacity is acceptable.
- Validate capacity at configuration load time and fail fast with a clear message.
When it happens
Trigger: Passing 0 explicitly; passing a negative number; computing capacity as `collection.size()` on an empty collection; wiring a config property that defaults to 0.
Common situations: Capacity read from a properties/YAML file with no default; sizing math with an off-by-one (`size - 1`); DI/bean configuration supplying an unset int field.
Related errors
- Queue is full
- capacity must greater than 0!
- Capacity must be greater than 0!
- Capacity cannot be negative.
- Element not in queue
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/8458ee3dd60d2609.
Report an issue: GitHub.