I'm trying to write a method that returns a sub lists and each containin the max value of strigns from a larger list.

I'm using iterator and backtrack recursion to achieve this, however - I'm not familiar with backtrack recursion. Any suggestions on how to make this work. Or if my code needs adjustments?

This is what I have so far:

private void printAnagrams(List<String> anagrams, int max, List<List<String>> listofLists) {
    Iterator<String> iterate = anagrams.iterator();
    String word = "";
    while (iterate.hasNext()) {
        for(int i = 0; i < anagrams.size(); i++) {
            word = iterate.next();
            listofLists.add(new ArrayList<>());
            listofLists.get(i).add(word);
            if (listofLists.size() == max) {
                listofLists.add(new ArrayList<>());
                    // Continue new list
            }
        }
    }
}

This is the method that calls the private method:

 public void printAnagrams(String phrase, int max, List<List<String>> anagrams) {
    if (anagrams == null || max < 0) { 
        throw new IllegalArgumentException(); 
    } else if (max == 0) {
        getWords(phrase);
    } else  
        printAnagrams(phrase, max, anagrams);
}// End of printAnagram method 

Sample output:

List:

[core, course, cs, cure, for, force, forces, four, of, off, offer, offers, or, our, ours, re, score, so, source, suffer, sure, us, use, user]

SubLists with max = 3:

[core, off, us]
[core, us, off]
[course, off]
[cure, off, so]
[cure, so, off]
[force, of, us]
[force, us, of]

I'm working on an Anagram Finder program. One of the methods I'm writing introduces the concept of recursive backtracking which I'm completely new to. I'm hoping to get some suggestions of how this works/how to make it work.

I'm trying to implement recursive backtracking in order to display a list of words from a dictionary text file to form anagrams for an input phrase. Here's what I have so far:

public void printAnagrams(String phrase, int max, List<List<String>> anagrams) {
    if (anagrams == null) { 
        throw new IllegalArgumentException(); 
    } else if (max != 0) {
        System.out.println("...");
    } else 
        System.out.println("...");
    for (List<String> element : anagrams)
        System.out.println(anagrams);
}// End of printAnagram method 

If max > 0, only combination of max words or less are displayed.
Since the program is pretty long, the background info is listed in the links below.

AnagramFinder class: Click Here

Client class: Click Here

This is the expected results:

File name? dict2.txt

Phrase to Scramble? off course

All words found in "off course":
[core, course, cs, cure, for, force, forces, four, of, off, offer, offers, or, our, ours, re, score, so, source, suffer, sure, us, use, user]

Max words to include (Enter for no max)? 3

[core, off, us]

[core, us, off]

[course, off]

[cure, off, so]

[cure, so, off]

[force, of, us]

[force, us, of]

[of, force, us]

[of, us, force]

[off, core, us]

[off, course]

[off, cure, so]

[off, so, cure]

[off, source]

[off, us, core]

[so, cure, off]

[so, off, cure]

[source, off]

[us, core, off] ...