{"record":{"id":"8efe7c84853fe81b","repo":"deeplearning4j/deeplearning4j","slug":"both-arrays-must-have-the-same-length","errorCode":null,"errorMessage":"Both arrays must have the same length","messagePattern":"Both arrays must have the same length","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"nd4j/nd4j-common/src/main/java/org/nd4j/common/util/ArrayUtil.java","lineNumber":2928,"sourceCode":"        int backWardCount = second.length - 1;\n        for (int i = 0; i < first.length; i++) {\n            if (first[i] != second[backWardCount--])\n                return false;\n        }\n        return true;\n    }\n\n    public static int[] plus(int[] ints, int mult) {\n        int[] ret = new int[ints.length];\n        for (int i = 0; i < ints.length; i++)\n            ret[i] = ints[i] + mult;\n        return ret;\n    }\n\n\n    public static int[] plus(int[] ints, int[] mult) {\n        if (ints.length != mult.length)\n            throw new IllegalArgumentException(\"Both arrays must have the same length\");\n        int[] ret = new int[ints.length];\n        for (int i = 0; i < ints.length; i++)\n            ret[i] = ints[i] + mult[i];\n        return ret;\n    }\n\n    public static int[] times(int[] ints, int mult) {\n        int[] ret = new int[ints.length];\n        for (int i = 0; i < ints.length; i++)\n            ret[i] = ints[i] * mult;\n        return ret;\n    }\n\n    public static int[] times(int[] ints, int[] mult) {\n        Preconditions.checkArgument(ints.length == mult.length, \"Ints and mult must be the same length\");\n        int[] ret = new int[ints.length];\n        for (int i = 0; i < ints.length; i++)\n            ret[i] = ints[i] * mult[i];","sourceCodeStart":2910,"sourceCodeEnd":2946,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/nd4j-common/src/main/java/org/nd4j/common/util/ArrayUtil.java#L2910-L2946","documentation":"ArrayUtil.plus(int[] ints, int[] mult) performs element-wise addition of two int arrays. It throws IllegalArgumentException('Both arrays must have the same length') when lengths differ, since element-wise arithmetic has no defined pairing for extra elements. A strict precondition for a helper that assumes equally-sized operands.","triggerScenarios":"Calling ArrayUtil.plus(a, b) where a.length != b.length, e.g. plus(new int[]{1,2}, new int[]{1,2,3}); typically adding a bias vector to activations of different size, or shape vs stride arrays.","commonSituations":"Configuration drift between two vector inputs (feature count vs coefficient count); broadcasting expectations from other frameworks that silently extend shorter arrays; truncated arrays after serialization/deserialization.","solutions":["Pad or trim one array so both have equal length before calling","Use Nd4j broadcast operations instead if implicit broadcasting semantics are desired","Fix the input generation so both vectors share the same dimension"],"exampleFix":"// before\nint[] sum = ArrayUtil.plus(activations, bias); // different lengths\n// after\nint[] sum = activations.length == bias.length\n        ? ArrayUtil.plus(activations, bias)\n        : ArrayUtil.plus(activations, Arrays.copyOf(bias, activations.length));","handlingStrategy":"validation","validationCode":"if (ints.length != mult.length) throw new IllegalArgumentException(\"plus() requires equal lengths: \" + ints.length + \" vs \" + mult.length);","typeGuard":"boolean sameLength(int[] a, int[] b) { return a != null && b != null && a.length == b.length; }","tryCatchPattern":"try { int[] sum = ArrayUtil.plus(ints, mult); } catch (IllegalArgumentException e) { /* pad/trim or use broadcast ops */ }","preventionTips":["Validate vector dimensions together with the config that sets them","Use Nd4j broadcasting for intentionally different shapes","Add dimension checks to the code that produces each operand"],"tags":["nd4j","arrays","shape-mismatch","elementwise"],"backgroundTag":"shape-mismatch","analyzedSha":"4c22ac5fe4a8350d05d224e7f4499429f7f69c93","analyzedAt":"2026-09-07T05:27:04.714Z","contentChangedAt":"2026-09-07T05:27:04.714Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}