-1

I am supposed to write a recursive method that deletes a reoccurrence of a number at the end of an arrayList. I feel like I've covered my bases with every possible error but I get an out of bounds error once it processes for the 3rd time. I can't seem to figure out why I get the out of bounds error. Seems like all of my counts are staying in the right positions, and I have an if-statement that uses recursion once the counter positions are equal, which is where I figured the error message would be.

Any insight would be helpful, need to learn from my mistakes. Recursion is not my strong-suit either.

EDIT: This is the list; [100, 200, 200, 300, 400, 300, 100, 500, 500, 400, 100, 400, 100, 100]

public static void deleteDuplicateValues(ArrayList<Integer> list, int decreasingCounter, int searchingVal, int outsideCounter)
  {
    int searchingValue = list.get(searchingVal);


    if (outsideCounter < (list.size()-1))
    {

      if (searchingValue == list.get(list.size()-1-decreasingCounter)) //finds
      {
        System.out.print (searchingValue + "   FOUND at position" + (list.size()-1-decreasingCounter) + "\n");
        list.remove(list.size()-1-decreasingCounter);

        deleteDuplicateValues(list, decreasingCounter,searchingVal+1, outsideCounter+1);

      }
      else
      {
        if (list.size()-1-decreasingCounter == outsideCounter) //gets to end without finding double
        {//After searching x amount of times, they will equal eachother if not found.
          //outsideCounter only increments when found or end of processing. 
          decreasingCounter = 0;
          deleteDuplicateValues(list, decreasingCounter,searchingVal+1, outsideCounter+1); //goes to next position
        }

        else 
        {
          System.out.print("executed");
          deleteDuplicateValues(list, decreasingCounter+1, searchingVal, outsideCounter); //values UP1


        }
      }
    }
user90000000009
  • 51
  • 1
  • 1
  • 5

2 Answers2

0

Did I get you wrong or you want to remove duplicates? If that's the case, why no use any of the implementations of java.util.Set??

Set<Integer> set = new HashSet<Integer>(list);
list.clear();
list.addAll(set);

I couldn't understand completely what was all that about the indexes and counts

EDIT: I think i got it. When you got to this if statement

if (searchingValue == list.get(list.size() - 1 - decreasingCounter)) {
    System.out.print(searchingValue + "   FOUND at position" + (list.size() - 1 - decreasingCounter) + "\n");
    list.remove(list.size() - 1 - decreasingCounter);

    deleteDuplicateValues(list, decreasingCounter, searchingVal + 1, outsideCounter + 1); <<-- HERE!
}

You remove an element, and even so, when you make the recursive call, you increase the serachingValue, BUT, you have already move, by removing the element. Did I make myself clear?

coya
  • 264
  • 2
  • 15
0

You can simplify your invariant. All you need in method parameters is a list of integer and the value of the number you are removing, only two params. I don't get why you need all others - try simplifying it you'll crack it.

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68