TheAlgorithms/Java · error · IllegalArgumentException
Value and weight arrays must be of the same length.
Error message
Value and weight arrays must be of the same length.
What it means
KnapsackZeroOne.compute indexes values[n-1] and weights[n-1] in the same recursion step, so the two arrays must have identical length. A mismatch would cause ArrayIndexOutOfBoundsException. The method rejects this with IllegalArgumentException upfront.
Source
Thrown at src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOne.java:35
}
/**
* Solves the 0/1 Knapsack problem using recursion.
*
* @param values the array containing values of the items
* @param weights the array containing weights of the items
* @param capacity the total capacity of the knapsack
* @param n the number of items
* @return the maximum total value achievable within the given weight limit
* @throws IllegalArgumentException if input arrays are null, empty, or
* lengths mismatch
*/
public static int compute(final int[] values, final int[] weights, final int capacity, final int n) {
if (values == null || weights == null) {
throw new IllegalArgumentException("Input arrays cannot be null.");
}
if (values.length != weights.length) {
throw new IllegalArgumentException("Value and weight arrays must be of the same length.");
}
if (capacity < 0 || n < 0) {
throw new IllegalArgumentException("Invalid input: arrays must be non-empty and capacity/n "
+ "non-negative.");
}
if (n == 0 || capacity == 0 || values.length == 0) {
return 0;
}
if (weights[n - 1] <= capacity) {
final int include = values[n - 1] + compute(values, weights, capacity - weights[n - 1], n - 1);
final int exclude = compute(values, weights, capacity, n - 1);
return Math.max(include, exclude);
} else {
return compute(values, weights, capacity, n - 1);
}
}
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Assert values.length == weights.length before calling compute.
- Build items as a single structure (e.g., a record list) and split to parallel arrays only at the call site.
- If sources diverge, investigate the upstream data pipeline rather than silently trimming.
Example fix
// before
int r = KnapsackZeroOne.compute(values, weights, cap, n); // throws if lengths differ
// after
if (values.length != weights.length) {
throw new IllegalStateException("Item data corrupted: length mismatch");
}
int r = KnapsackZeroOne.compute(values, weights, cap, n); Defensive patterns
Strategy: validation
Validate before calling
if (values.length != weights.length) {
throw new IllegalStateException("Values and weights arrays must match in length");
}
int r = KnapsackZeroOne.compute(values, weights, capacity, n); Type guard
static boolean areMatchingLengths(int[] values, int[] weights) {
return values != null && weights != null && values.length == weights.length;
} Prevention
- Maintain items as a single collection and split to parallel arrays atomically.
- Investigate upstream pipelines when array lengths diverge rather than silently trimming.
When it happens
Trigger: Passing values and weights arrays that were populated independently and ended up with different element counts.
Common situations: Loading item data from two separate queries or files that diverge in row count; appending to one list but not the other; filtering one array without applying the same filter to the other.
Related errors
- Weights and values must be non-null and of the same length.
- Values and weights arrays must be non-null and of same lengt
- Weight capacity should not be negative.
- Weights must be positive.
- Input arrays cannot be null.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/f7a6a8bd808a7a46.
Report an issue: GitHub.