{"record":{"id":"0f13399f630b4d3d","repo":"TheAlgorithms/Java","slug":"array-contains-negative-integers","errorCode":null,"errorMessage":"Array contains negative integers.","messagePattern":"Array contains negative integers\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/sorts/PigeonholeSort.java","lineNumber":44,"sourceCode":"        final int maxElement = Arrays.stream(array).max().orElseThrow();\n        final List<List<Integer>> pigeonHoles = createPigeonHoles(maxElement);\n\n        populatePigeonHoles(array, pigeonHoles);\n        collectFromPigeonHoles(array, pigeonHoles);\n\n        return array;\n    }\n\n    /**\n     * Checks if the array contains any negative integers.\n     *\n     * @param array the array to be checked\n     * @throws IllegalArgumentException if any negative integers are found\n     */\n    private static void checkForNegativeInput(int[] array) {\n        for (final int number : array) {\n            if (number < 0) {\n                throw new IllegalArgumentException(\"Array contains negative integers.\");\n            }\n        }\n    }\n\n    /**\n     * Creates pigeonholes for sorting using an ArrayList of ArrayLists.\n     *\n     * @param maxElement the maximum element in the array\n     * @return an ArrayList of ArrayLists\n     */\n    private static List<List<Integer>> createPigeonHoles(int maxElement) {\n        List<List<Integer>> pigeonHoles = new ArrayList<>(maxElement + 1);\n        for (int i = 0; i <= maxElement; i++) {\n            pigeonHoles.add(new ArrayList<>());\n        }\n        return pigeonHoles;\n    }\n","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/sorts/PigeonholeSort.java#L26-L62","documentation":"Thrown by PigeonholeSort.checkForNegativeInput(int[]) when any element is negative. Pigeonhole sort allocates one hole per value in [0, max], so negative indices would fall outside the hole array. The private check is invoked during sort and rejects negatives before hole creation.","triggerScenarios":"Sorting an array like {3, -1, 0}; passing counts or indices that went negative; merging datasets containing negatives.","commonSituations":"Signed metric data; off-by-one producing a negative index; using PigeonholeSort on general-purpose integer arrays.","solutions":["Validate all elements >= 0 before calling the sort.","Shift values by a known offset to make them non-negative, or pick a different sort.","Use Arrays.sort for signed integers when the non-negative guarantee cannot be enforced."],"exampleFix":"// before\nPigeonholeSort.sort(data);\n\n// after\nif (IntStream.of(data).anyMatch(v -> v < 0)) throw new IllegalArgumentException(\"negatives not supported\");\nPigeonholeSort.sort(data);","handlingStrategy":"validation","validationCode":"if (IntStream.of(array).anyMatch(v -> v < 0)) throw new IllegalArgumentException(\"negatives not supported by PigeonholeSort\");","typeGuard":"public static boolean isAllNonNegative(int[] a) { return IntStream.of(a).allMatch(v -> v >= 0); }","tryCatchPattern":null,"preventionTips":["Validate the non-negative domain at the data-loading boundary.","Offset signed data by a known constant when the range is bounded.","Use Arrays.sort for general signed integers."],"tags":["sorting","non-negative-domain","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}