0-1 Knapsack in Java
int[][] maxbenefit = new int[weights.length + 1][capacity + 1];

    for (int i = 0; i &lt;= weights.length; i++)
        maxbenefit[i][0] = 0;
    for (int i = 0; i &lt;= capacity; i++)
        maxbenefit[0][i] = 0;


There is no need to initialize part of the array as 0, because the entire array starts off as 0.

public static void printMatrix(int[][] matrix, int R, int C) {
    for (int i = 0; i &lt;= R; i++) {
        for (int j = 0; j &lt;= C; j++) {

            System.out.print(matrix[i][j] + " | ");

        }

        System.out.println();
    }

}


In here, you should check if R and C don't exceed matrix.length and matrix[i].length respectively. That way you can either limit the printing, or you could throw an error with explanation, rather than running into ArrayIndexOutOfBoundsException.

In the same way, you should check the size of int[] benefits in getMaxBenefit, because if the weights and the benefits are not the same size then you'll throw an error at some point, and failing fast is better.