检查arraylist元素是否非递减的递归方法

时间:2017-03-15 17:45:01

标签: java recursion

我的方法出现了stackoverflow错误,我不知道为什么,因为我的

if (index < elements.size())

行确保它不是无限递归调用。到目前为止这是我的代码。

 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;
 }

我不知道为什么我收到错误

0 个答案:

没有答案