emmynaki
emmynaki

Reputation: 157

recursion issue beginner simple java

I'm getting a stackoverflow error on my method and I'm not sure why because my

if (index < elements.size())

line makes sure that it's not an infinite recursive call. This is my code so far.

 private boolean checkIfIncreasing(ArrayList<T> elements, int index){ 
  index = 0;
  boolean currentReturnVal = false; 

  //element at position 0 of the passed in array 
  T objAtIndex = elements.get(index); 

  //element at position 1 of the passed in array 
  T objAtNextIndex = elements.get(index + 1);

  //if the size is 1 then just return true bc its the only element in there 
  if (elements.size() == 1){ currentReturnVal = true;}

  if (index < elements.size()){ //takes care of non infinite "looping"

     //checks to see if obj at index 0 is less than or equal to obj 1
     if (objAtIndex.compareTo(objAtNextIndex) <= 0){ 
        currentReturnVal = true;}

     checkIfIncreasing(elements, index++);
     if (objAtIndex.compareTo(objAtNextIndex) >= 0){ 
        return false; } 
  }
  return currentReturnVal;
 }

I don't know why i'm receiving the error and I don't know how to structurally fix it.

Upvotes: 2

Views: 77

Answers (4)

Grzegorz G&#243;rkiewicz
Grzegorz G&#243;rkiewicz

Reputation: 4586

Keep it simple and short.

 private boolean checkIfIncreasing(List<T> elements, int index) {
  if (elements.size() < 2 || index + 1 == elements.size())
      return true;
  if (elements.get(index).compareTo(elements.get(index+1)) < 1)
      return checkIfIncreasing(elements, index+1);
  return false;
}

This should be called with index 0.

Upvotes: 1

Ousmane D.
Ousmane D.

Reputation: 56433

I don't know why i'm receiving the error and I don't know how to structurally fix it.

You're resetting index to 0 at each invocation of the method hence this condition if (index < elements.size()) is will always true unless elements.size() is 0 in which case the code inside the if block will keep executing checkIfIncreasing(elements, index++); repeatedly at each invocation of the method and eventually the StackOverflow Exception will be thrown.

to prevent the StackOverflow Exception simply remove this line: index = 0;

Upvotes: 0

Beri
Beri

Reputation: 11610

You need to increment index before passing to your recursive function

private boolean checkIfIncreasing(ArrayList<T> elements, int index){ 
 // check if array has at least 2 elements first, ot you will get an exception
  if (elements.size() <=1 || index >= elements.size() ){ return true;}

  //checks to see if obj at index 0 is less than obj 1
  if (objAtIndex.compareTo(objAtNextIndex) < 0){ 
      // if any obj[x] is smaller than obj[x+1]
      return false;
  }
 // here element index and index+1 are either in incremental order or equal
    return  checkIfIncreasing(elements, ++index);     
}

Upvotes: 1

Jay Schauer
Jay Schauer

Reputation: 487

In the first line of the method you set the index to 0.

So even though you increment it before recursing, it will always be set to 0.

Instead what you might want is a helper method, like this:

private boolean checkIfIncreasing(ArrayList<T> elements) {
    return checkIfIncreasing(elements, 0);
}

And then remove the first line in your checkIfIncreasing(ArrayList<T> elements, int index) method

This technique is known as overloading methods.

Upvotes: 0

Related Questions