{"record":{"id":"b2099ba301cccb5a","repo":"TheAlgorithms/Java","slug":"capacity-cannot-be-negative","errorCode":null,"errorMessage":"Capacity cannot be negative.","messagePattern":"Capacity cannot be negative\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java","lineNumber":35,"sourceCode":" *\n * @param <E> the type of elements that this array can hold\n */\npublic class DynamicArray<E> implements Iterable<E> {\n\n    private static final int DEFAULT_CAPACITY = 16;\n    private int size;\n    private int modCount; // Tracks structural modifications for iterator integrity\n    private Object[] elements;\n\n    /**\n     * Constructs a new DynamicArray with the specified initial capacity.\n     *\n     * @param capacity the initial capacity of the array\n     * @throws IllegalArgumentException if the specified capacity is negative\n     */\n    public DynamicArray(final int capacity) {\n        if (capacity < 0) {\n            throw new IllegalArgumentException(\"Capacity cannot be negative.\");\n        }\n        this.size = 0;\n        this.modCount = 0;\n        this.elements = new Object[capacity];\n    }\n\n    /**\n     * Constructs a new DynamicArray with a default initial capacity.\n     */\n    public DynamicArray() {\n        this(DEFAULT_CAPACITY);\n    }\n\n    /**\n     * Adds an element to the end of the array. If the array is full, it\n     * creates a new array with double the size to accommodate the new element.\n     *\n     * @param element the element to be added to the array","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java#L17-L53","documentation":"The DynamicArray constructor rejects negative capacity because the backing Object[] array cannot have a negative length. Java would throw an obscure NegativeArraySizeException otherwise; this guard provides a clearer, catchable IllegalArgumentException. A capacity of 0 is permitted (creates an empty backing array that grows on demand).","triggerScenarios":"Calling new DynamicArray<>(-1) or passing any negative int to the constructor. Can also arise from arithmetic that produces a negative result.","commonSituations":"Capacity derived from a subtraction or a computed size that goes negative on edge cases. Config values or user input passed through without validation.","solutions":["Validate that capacity >= 0 before constructing","Use the no-arg constructor (defaults to capacity 16) when you do not have a specific size requirement","Clamp to 0 if negative capacity is possible from the computation"],"exampleFix":"// before\nDynamicArray<String> arr = new DynamicArray<>(requestedSize - buffer);\n\n// after\nint cap = Math.max(0, requestedSize - buffer);\nDynamicArray<String> arr = new DynamicArray<>(cap);","handlingStrategy":"validation","validationCode":"int cap = Math.max(0, requestedCapacity);\nDynamicArray<String> arr = new DynamicArray<>(cap);","typeGuard":null,"tryCatchPattern":"try {\n    arr = new DynamicArray<>(capacity);\n} catch (IllegalArgumentException e) {\n    arr = new DynamicArray<>(); // default capacity 16\n}","preventionTips":["Validate capacity >= 0 before construction","Use the no-arg constructor for the default capacity of 16","Clamp computed sizes with Math.max(0, value)"],"tags":["dynamic-array","validation","constructor"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}