Hi In this recursion method i am trying to find all anagrams and add it to a Lis
ID: 3654846 • Letter: H
Question
Hi In this recursion method i am trying to find all anagrams and add it to a List> but what happens when i run this code is it just returns alot of empty Lists. [code] private List> findAnagrams(LetterInventory words, ArrayList anagram, int max, Map smallDict, int level, List> result) { ArrayList solvedWord = new ArrayList(); LetterInventory shell; LetterInventory shell2; if (level < max || max == 0) { Iterator it = smallDict.keySet().iterator(); while (it.hasNext()) { String k = it.next(); shell = new LetterInventory(k); shell2 = words; if (shell2.subtract(shell) != null) { anagram.add(k); shell2 = words.subtract(shell); if (shell2.isEmpty()) { //System.out.println(anagram.toString()); it prints off fine here result.add(anagram); // but doesnt add here } else findAnagrams(shell2, anagram, max, smallDict, level + 1, result); anagram.remove(anagram.size()-1); } } } return results; } [/code]Explanation / Answer
Cant but sure if this solves all of your problems but it should certainly help with getting something to return. You are missing a return statement, or something, in your else statement. So basically every time shell 2 is not empty, it does all of this work and does not return all of its results it is doing. In any recursive statement you should always be doing something with a call to itself as this will not be reflected entirely at the end of it. If these issues are still happening after you change this please post all of the code so i can actually go through the logic more instead of just having to take my best guess. Also post it to codepad.org or a site where you can keep the formatting, as even this code was not fun to do until it was spaced out. http://codepad.org/5WfMUlFc
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.