TheAlgorithms/Java · error · IllegalArgumentException

x and y arrays must have the same length.

Error message

x and y arrays must have the same length.

What it means

Thrown by Neville.interpolate(double[] x, double[] y, double target) when the x and y arrays have different lengths. Neville's algorithm interpolates a polynomial through (x, y) points; mismatched arrays represent an inconsistent point set. This check runs before the emptiness check, so length mismatch is detected first.

Source

Thrown at src/main/java/com/thealgorithms/maths/Neville.java:35

public final class Neville {

    private Neville() {
    }

    /**
     * Evaluates the polynomial that passes through the given points at a
     * specific x-coordinate.
     *
     * @param x The x-coordinates of the points. Must be the same length as y.
     * @param y The y-coordinates of the points. Must be the same length as x.
     * @param target The x-coordinate at which to evaluate the polynomial.
     * @return The interpolated y-value at the target x-coordinate.
     * @throws IllegalArgumentException if the lengths of x and y arrays are
     * different, if the arrays are empty, or if x-coordinates are not unique.
     */
    public static double interpolate(double[] x, double[] y, double target) {
        if (x.length != y.length) {
            throw new IllegalArgumentException("x and y arrays must have the same length.");
        }
        if (x.length == 0) {
            throw new IllegalArgumentException("Input arrays cannot be empty.");
        }

        // Check for duplicate x-coordinates to prevent division by zero
        Set<Double> seenX = new HashSet<>();
        for (double val : x) {
            if (!seenX.add(val)) {
                throw new IllegalArgumentException("Input x-coordinates must be unique.");
            }
        }

        int n = x.length;
        double[] p = new double[n];
        System.arraycopy(y, 0, p, 0, n); // Initialize p with y values

        for (int k = 1; k < n; k++) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate that x.length == y.length before calling interpolate
  2. Ensure data ingestion produces paired (x, y) entries atomically rather than separate arrays
  3. Add a unit test that verifies array-length equality for interpolation inputs

Example fix

// before
double result = Neville.interpolate(xCoords, yCoords, target);

// after
if (xCoords.length != yCoords.length) {
    throw new IllegalArgumentException("x and y must have equal length");
}
double result = Neville.interpolate(xCoords, yCoords, target);
Defensive patterns

Strategy: validation

Validate before calling

if (x.length != y.length) {
    throw new IllegalArgumentException("x and y arrays must have equal length");
}
double result = Neville.interpolate(x, y, target);

Type guard

static boolean arePairedArrays(double[] x, double[] y) {
    return x != null && y != null && x.length == y.length;
}

Try / catch

try {
    double result = Neville.interpolate(x, y, target);
} catch (IllegalArgumentException e) {
    // x and y length mismatch — check data pipeline
    logger.error("Interpolation input mismatch: x.length={}, y.length={}", x.length, y.length);
}

Prevention

When it happens

Trigger: Calling interpolate with x.length != y.length, e.g., interpolate(new double[]{1,2,3}, new double[]{1,2}, 1.5). Common when x and y come from different data sources or when one array is truncated.

Common situations: Reading x and y coordinates from separate columns or API responses where one has missing entries. Copy-paste errors in test data. Off-by-one in array slicing that affects only one array.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/4860f9f1df303744. Report an issue: GitHub.