Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java (Java)

Code before:
     *
     * @param weightCapacity The maximum weight capacity of the knapsack.
     * @param weights        An array of item weights.
     * @param values         An array of item values.
     * @return The maximum value that can be obtained without exceeding the weight capacity.
     * @throws IllegalArgumentException If the input arrays are null or have different lengths.
     */
    public static int knapSack(final int weightCapacity, final int[] weights, final int[] values) throws IllegalArgumentException {
        if (weightCapacity < 0) {
            throw new IllegalArgumentException("Weight capacity should not be negative.");
        }
        if (weights == null || values == null || weights.length != values.length) {
            throw new IllegalArgumentException("Input arrays must not be null and must have the same length.");
        }
        for (int w : weights) {
            if (w <= 0) {
                throw new IllegalArgumentException("Input array should not contain non-positive weight(s).");
            }
        }

        // DP table to store the state of the maximum possible return for a given weight capacity.
        int[] dp = new int[weightCapacity + 1];

        for (int i = 0; i < values.length; i++) {
            for (int w = weightCapacity; w > 0; w--) {
                if (weights[i] <= w) {
                    dp[w] = Math.max(dp[w], dp[w - weights[i]] + values[i]);
                }
            }
        }

        return dp[weightCapacity];
    }
}


Review comment (suggestion):
If you insist on having this comment, why not to create a method like `private static int[] initializeDpArray(final int weightCapacity)`;

If you decide to have it, please check here if `weightCapacity >= 0` (not in the method `checkInput` [mentioned earlier](https://github.com/TheAlgorithms/Java/pull/4368#discussion_r1328099633)). 

Code after:
    private static void throwIfInvalidInput(final int weightCapacity, final int[] weights, final int[] values) {
        if (weightCapacity < 0) {
            throw new IllegalArgumentException("Weight capacity should not be negative.");
        }
        if (weights == null || values == null || weights.length != values.length) {
            throw new IllegalArgumentException("Input arrays must not be null and must have the same length.");
        }
        if (Arrays.stream(weights).anyMatch(w -> w <= 0)) {
            throw new IllegalArgumentException("Input array should not contain non-positive weight(s).");
        }
    }

    /**
     * Solves the 0-1 Knapsack problem using Dynamic Programming.
     *
     * @param weightCapacity The maximum weight capacity of the knapsack.
     * @param weights        An array of item weights.
     * @param values         An array of item values.
     * @return The maximum value that can be obtained without exceeding the weight capacity.
     * @throws IllegalArgumentException If the input arrays are null or have different lengths.
     */
    public static int knapSack(final int weightCapacity, final int[] weights, final int[] values) throws IllegalArgumentException {
        throwIfInvalidInput(weightCapacity, weights, values);

        // DP table to store the state of the maximum possible return for a given weight capacity.
        int[] dp = new int[weightCapacity + 1];

        for (int i = 0; i < values.length; i++) {
            for (int w = weightCapacity; w > 0; w--) {
                if (weights[i] <= w) {
                    dp[w] = Math.max(dp[w], dp[w - weights[i]] + values[i]);
                }
            }
        }

        return dp[weightCapacity];
    }
}