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]

This is the method that calls the private method:

I see no call from the public printAnagrams function to the private function printAnagrams? I see no call anywhere to the private printAnagrams function. What am I missing? I see a recursive call from the public printAnagrams function to itself and it appears, at least to me, to be infinite recursion since the public printAnagrams function changes no parameters.

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

This is a List of Strings, all the words that can be made up from "off course", correct? Is this list an input parameter to one of the functions? Is this list created from a call to the getWords function, which is called if max equals 0? If so, how? I see getWords takes a String and no return value is harnessed, so is it a void function?

Do you need two threads? If not, please add any pertinent information from the other thread to this one and mark the other one solved.

I see the desired result:

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

I see seven ordered subsets of your list of words. I see [core, off, us] and [core, us, off], so order must count, but what about [off, core, us] and the other three orderings?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.