Here is my code first:
public static List<AnagramFamily> assignFamilies(List<Word> theOriginalList) {
//Stores the list of anagram Families.
List<AnagramFamily> anagramList = new LinkedList<AnagramFamily>();
//TempList to store the word objects.
List<Word> tempList = new LinkedList<Word>();
ListIterator<Word> itr = theOriginalList.listIterator();
Word firstWord = theOriginalList.get(0);
int cnt = 0;
while(itr.hasNext()) {
Word secondWord = itr.next();
if (firstWord.getMyCanonical().equals(secondWord.getMyCanonical())) {
tempList.add(secondWord);
} else {
cnt++;
Collections.sort(tempList, new WordComparatorByDesecending());
anagramList.add(new AnagramFamily(tempList));
System.out.println(anagramList);
tempList.clear();
secondWord = itr.previous();
//create new anagram family
}
System.out.println("New First Word: " + firstWord + " Second Word: " + secondWord);
firstWord = secondWord;
}
System.out.println(anagramList);
return anagramList;
}
}
So basically what is going on here is that i am taking in a linked list of word objects which are: [abt, act, act, act, apt, apt, apt, at, ehy, ehy, ehy] Next I make a new Anagram Family object which will store it in decending order. Within the list called anagramList to where if I want I can iterate through it and print the results.
Here are the correct results:[[abt][act, act, act] [apt, apt, apt] [ehy, ehy, ehy]]
My problem here my output looks like this [[ehy, ehy, ehy], [ehy, ehy, ehy], [ehy, ehy, ehy], [ehy, ehy, ehy]]
My problem is in this line of code anagramList.add(new AnagramFamily(tempList)); because every time it overwrites the last object. How would I do this so it doesnt overwrite it? If someone could help that would be great thankyou!
public class AnagramFamily {
private List<Word> myFamilyList;
private int myFamilySize;
public AnagramFamily(List<Word> theFamilyList) {
myFamilyList = theFamilyList;
myFamilySize = myFamilyList.size();
}
public int getFamilySize() {
return myFamilySize;
}
public String toString() {
return myFamilyList.toString();
}
}
The final answer would be to do tempList = new LinkedList<Word>();
This will keep a new instance of it instead of overriding the object each time.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments