javaknapsack-problem

Array out of bounds exception in Java while implementing the knapsack algorithm


I am a beginner to Java programming, and I was trying to implement knapsack algorithm by dynamic programming and it works perfectly for few inputs, but for few inputs it throws an exception. The code is

package knapsack3;

import java.util.Scanner;
 
/** Class Knapsack **/

public class knapsack3
{
    
   
    public void solve(int wt[], int val[], int M, int n)
    {
        int nob = 25;
        int i,j;
        int v[][]=new int[nob][nob];
        for(i=0;i<=n;i++)
        {
            for(j=0;j<=M;j++)
            {
                if(i==0||j==0)
                {
                    v[i][j]=0;
                }
                else if(j-wt[i]<0)
                {
                    v[i][j]=v[i-1][j];
                }
                else
                {
                    v[i][j]=Math.max(v[i-1][j],val[i]+v[i-1][j-wt[i]]);
                }
            }
        }
        System.out.println("\n Final Profit(value)---> "+v[n-1][M]);
    }
    /** Main function **/
    public static void main (String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Knapsack Algorithm Test\n");
  
        knapsack3 ks = new knapsack3();
        
        System.out.println("Enter number of elements ");
        int n = scan.nextInt();
 
        int wt[] = new int[n + 1];
        int val[] = new int[n + 1];
 
        System.out.println("\nEnter weight for "+ n +" elements");
        for (int i = 1; i <= n; i++)
            wt[i] = scan.nextInt();
        
        System.out.println("\nEnter value for "+ n +" elements");
        for (int i = 1; i <= n; i++)
            val[i] = scan.nextInt();
 
        System.out.println("\nEnter knapsack weight ");
        int M = scan.nextInt();

        ks.solve(wt, val, M, n);
       scan.close();
    }
    
}

the exception is

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 25 at knapsack3.knapsack3.solve(knapsack3.java:22) at knapsack3.knapsack3.main(knapsack3.java:61)


Solution

  • Because of your array

    int nob = 25;
    int v[][]=new int[nob][nob];
    

    is hardcoded to size 25, but you then use a for-loop that goes to n and M (So if you enter a number for n or M larger than nob aka 25 then you will get an out of bounds exception.

    Your for-loops should always use the array in them like this

    for(int i = 0; i < v.length; i++)
    {
        for(int j = 0; j < v[i].length; j++){}
    }
    

    to avoid this issue.

    edit However, Your real issue, now that I re look at the question is that your for-loop does this:

    for(int i = 0; i <= n; i++)
    

    If you have i <= n starting at 0 you will run this loop 26 (not 25) times. Since I am assuming your n is 25 since you hard-coded nob to 25.

    Also

    for(int j = 0; j <= M; j++)
    

    means that you will go from 0 to M (Knapsack size) which I assume is almost always above 25, so this will always cause an out of bounds exception because you hard code the size of the array.