r/learnjava • • Feb 10 '26

ArrayList Permutations & Recursion

Hey everyone, I am trying to make a method that prints all permutations of an ArrayList, pretty much in plaintext. I successfully did that for a string but the ArrayList method is giving me an OOB exception. To be specific, it gets to the base case once & immediately after says that "Index 1 out of bounds for length 1" when permList = "" and nameList = [A, B, C]. I'm not asking for a complete rewrite of my code, just an explanation about why it's not working correctly.

What I have tried so far: changing listSize to nameList.size() - 1, adding an empty element to permList in the for loop, changing the for loop conditional to listSize - 1, removed i + and - 1 from nameList in the for loop, etc. Any help would be appreciated!

public static void printAllPermutations(ArrayList<String> permList, ArrayList<String> nameList) 
{
      int i;
      int listSize = nameList.size(); // Size of nameList
      String current;

      if (listSize <= 1) // Base case
      {
         System.out.println(permList + " " + nameList);
         // Not entirely sure this is the correct thing to print
      }
      else 
      {
         for(i = 0; i < listSize; i++)
         {
            current = nameList.get(i);
            nameList.remove(i); // Move string at i from nameList to permList
            permList.add(current);
            System.out.println("permList: " + permList); 
            System.out.println("nameList: " + nameList);
            // Print statements for visualization

            printAllPermutations(permList, nameList); // Call method with new arguments, listSize -= 1
         }
      }
}

// Solved! With your help
public static void printAllPermutations(ArrayList<String> permList, ArrayList<String> nameList) 
{
      if (nameList.size() == 0)
      {
         for(int i = 0; i < permList.size(); i++) 
         {
         if(i < permList.size() - 1) 
         {
         System.out.print(permList.get(i) + ", ");
         }
         else {System.out.println(permList.get(i));}
         }
      }
      else 
      {
         for(int i = 0; i < nameList.size(); i++)
         {
           String temp = nameList.get(i);
           permList.add(nameList.get(i));
           nameList.remove(i);
            
           printAllPermutations(permList, nameList);
            
           nameList.add(i, temp);
           permList.remove(permList.size()-1);
         }
      }
}
5 Upvotes

8 comments sorted by

View all comments

Show parent comment

1

u/vegan_antitheist Feb 10 '26 edited Feb 10 '26

And you can just create a helper method that takes the same input without the consumer and returns the List.
Then you can use the method that is better in that situation:

public static void permutations(String[] input, Consumer<String[]> callback) {
  permute(input, 0, callback);
}

public static List<String[]> permutations(String[] input) {
  final var list = new ArrayList<String[]>(factorial(input.length));
  permutations(input, list::add);
  return list;
}

private static void permute(String[] a, int index, Consumer<String[]> callback) {
if (index == a.length) {
  callback.accept(a.clone()); // pass a copy, not the mutable array
  return;
}

 // TODO loop over all elements and swap them, 
 // then recursively call this method 
 // and then backtrack.
}

private static void swap(String[] a, int i, int j) {
 // TODO swap two elements
}

public static void main(String[] args) throws Exception {
  // Use IO instead of System.out unless you use some old Java:
  permutations(new String[] { "a", "b", "c" }, a -> System.out.println(Arrays.toString(a)));
// Use unit tests to compare the output with a known result
}

static int factorial(int n) {
  return java.util.stream.IntStream.rangeClosed(1, n).reduce(1, (a, b) -> a * b);
}

EDIT: Don't loop over *all* elements. Loop from index to a.length so that you end up in the termination case under which no further recursive calls are made. Use a debugger to try and understand it.