Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Checking for anagram

843785Nov 5 2008 — edited Nov 5 2008
Hi, i am prettry sure that my idea is correct, the principle being to turn the parameter to a char array and the word in the dictionary to a char array, sort them alphabetically then compare, if the same then woot :d

however i am aware that the for each loop in this instance is inappropriate as it will continually change the value of myAnagramTwo until it gets to the end

please can i have hints as to a better way of assigning myAnnagramTwo to a char array in order to compare them?

thanks everyone

(no solutions please as my teacher will go crazy)

        public ArrayList<String> anagrams(String word)
    {
            
        ArrayList<String>anagram;
        anagram = new ArrayList<String>();  
 
        
        char [] myAnagram = word.toCharArray();
        char [] myAnagramTwo = null;
        
            for (String otherWord : dictionary){
                myAnagramTwo = otherWord.toCharArray(); 
            }
            Arrays.sort(myAnagramTwo);
            Arrays.sort(myAnagram);
            
            if (myAnagram.equals(myAnagramTwo)){
                anagram.add(word);
            }
          
        return anagram;
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 3 2008
Added on Nov 5 2008
3 comments
199 views