LaVOZs

The World’s Largest Online Community for Developers

'; recursion - Recursive method in java stack overflow - LavOzs.Com

I am getting a stackoverflow in the following code:

private static boolean haveBeen(int k, int t,ArrayList <int[]> arr, int i){
    if(!(i==arr.size ())) {
        if (arr.get (i)[0] == k && arr.get (i)[1] == t)
            return true;
        return (haveBeen (k, t, arr, i + 1));
    }
    return false;
}

Arrays in arr are all populated with (populated) int[2]. (Trying to iterate over array to see if k,t do exist). TIA

//prince needs to catch villain in shortest path from initial coordinates i,j, in 
//square matrix of non negative ints .Can move to adjacent squares if 
//-2<(square-adjacent square)<3. returns -1 if no path exists. villain is -1.
public static int prince(int[][] drm, int i, int j){
        ArrayList<int[]> firstSquare = new ArrayList<int[]>();
        int[] currentCoordinates = {i,j};
        firstSquare.add (currentCoordinates);
        int returnValue = recursePrince (drm,i,j,0,0,firstSquare) ;
        if (returnValue == drm.length*drm.length)
           return -1;
        return returnValue;
    }


    private static int recursePrince(int[][] drm, int i, int j, int nextSquareX, int nextSquareY, ArrayList <int[]> visitedSquares) {
        int minPathLength=drm.length*drm.length;
        int[] currentCoordinates = {i,j};
        visitedSquares.add (currentCoordinates);
        ArrayList<int[]> visitedSquaresCopy = (ArrayList<int[]>) visitedSquares.clone ();
        if (drm[nextSquareX][nextSquareY]==-1) //exit condition
            return 0;
        if (drm.length > i+1)//go right
            if (isLegal (drm[i][j], drm[i + 1][j]) && !(haveBeen (i + 1, j, visitedSquaresCopy, 0))) {
                minPathLength = Math.min (recursePrince (drm, i, j, i + 1, j, visitedSquaresCopy) + 1, minPathLength);
            }        if (i-1 > -1)//go left
            if (isLegal (drm[i][j], drm[i - 1][j]) && !(haveBeen (i -1, j, visitedSquaresCopy, 0)))
                minPathLength = Math.min (recursePrince (drm, i, j, i - 1, j, visitedSquaresCopy)+1,minPathLength);
        if (drm[0].length> j+1)//go up
            if (isLegal (drm[i][j], drm[i][j+1]) && !(haveBeen (i, j+1, visitedSquaresCopy, 0)))
                minPathLength = Math.min (recursePrince (drm, i, j, i, j+1, visitedSquaresCopy)+1,minPathLength);
        if (j-1>-1)//go down
            if (isLegal (drm[i][j], drm[i][j-1]) && !(haveBeen (i, j-1, visitedSquaresCopy, 0)))
                minPathLength = Math.min (recursePrince (drm, i, j, i , j-1, visitedSquaresCopy)+1,minPathLength);
        return Math.min(drm.length*2,minPathLength);// Larger than max possible path so will represent NO PATH FOUND.
    }
    private static boolean haveBeen(int k, int t,ArrayList <int[]> arr, int i){
        if(!(i==arr.size ())) {
            if (arr.get (i)[0] == k && arr.get (i)[1] == t)
                return true;
            return (haveBeen (k, t, arr, i + 1));
        }
        return false;
    }
     private static boolean isLegal(int currRoofVal,int adjcRoofVal){
        return ((currRoofVal - adjcRoofVal<=2) && (currRoofVal-adjcRoofVal>=0)) || (adjcRoofVal-currRoofVal==1) || adjcRoofVal==-1;

     }
Related
What is tail recursion?
Is Java “pass-by-reference” or “pass-by-value”?
How do I efficiently iterate over each entry in a Java Map?
Does a finally block always get executed in Java?
What is the difference between public, protected, package-private and private in Java?
How do I read / convert an InputStream into a String in Java?
When to use LinkedList over ArrayList in Java?
How do I generate random integers within a specific range in Java?
How do I convert a String to an int in Java?
How to create a memory leak in Java?