Creating new objects using a iterator in a while loop

ProFesh

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();
  }
}
ProFesh

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Best way to avoid redundant code while creating multiple new objects

From Java

How to create new objects using loop

From Java

Creating multiple objects using for loop

From Dev

Creating new object in loop

From Dev

Using for loop iterator for if condition

From Dev

Creating a new column using for/nested loop in r

From Dev

While loop creating infinite loop

From Dev

Creating folders from user input using while or do while loop

From Dev

Creating a stack and iterator that runs thorugh the stack using a for loop

From Dev

Is there a simple way of creating a list of identical objects without using a loop?

From Dev

While loop not behaving as expected using Dates objects

From Dev

Creating objects using a loop (Powershell GUI)

From Dev

The correctness of creating objects using a loop

From Dev

While Creating a new project

From Dev

Creating multiple objects in loop

From Dev

Defining and appending in new list inside a loop using iterator (python)

From Dev

Creating a vector from other objects using 'for loop'

From Dev

Creating new columns using a for loop

From Dev

Creating multiple class objects using a loop

From Dev

Creating ATM with while loop

From Dev

How can you access methods of an iterator while using it in a for loop?

From Dev

Creating a new column in pandas using for loop?

From Dev

creating a function using for loop then mutate a new column using the created function

From Dev

input buffer while creating multiple objects using for loop in java

From Dev

More efficiency creating a new variable using for loop

From Dev

creating a time dataset by using mySql while loop

From Dev

How to create a loop for a search query using set coordinates from a data frame, while creating a new data frame for each search result?

From Dev

Creating new columns using loop in R

From Dev

Creating new objects using values ​in array using the for(of) function

Related Related

  1. 1

    Best way to avoid redundant code while creating multiple new objects

  2. 2

    How to create new objects using loop

  3. 3

    Creating multiple objects using for loop

  4. 4

    Creating new object in loop

  5. 5

    Using for loop iterator for if condition

  6. 6

    Creating a new column using for/nested loop in r

  7. 7

    While loop creating infinite loop

  8. 8

    Creating folders from user input using while or do while loop

  9. 9

    Creating a stack and iterator that runs thorugh the stack using a for loop

  10. 10

    Is there a simple way of creating a list of identical objects without using a loop?

  11. 11

    While loop not behaving as expected using Dates objects

  12. 12

    Creating objects using a loop (Powershell GUI)

  13. 13

    The correctness of creating objects using a loop

  14. 14

    While Creating a new project

  15. 15

    Creating multiple objects in loop

  16. 16

    Defining and appending in new list inside a loop using iterator (python)

  17. 17

    Creating a vector from other objects using 'for loop'

  18. 18

    Creating new columns using a for loop

  19. 19

    Creating multiple class objects using a loop

  20. 20

    Creating ATM with while loop

  21. 21

    How can you access methods of an iterator while using it in a for loop?

  22. 22

    Creating a new column in pandas using for loop?

  23. 23

    creating a function using for loop then mutate a new column using the created function

  24. 24

    input buffer while creating multiple objects using for loop in java

  25. 25

    More efficiency creating a new variable using for loop

  26. 26

    creating a time dataset by using mySql while loop

  27. 27

    How to create a loop for a search query using set coordinates from a data frame, while creating a new data frame for each search result?

  28. 28

    Creating new columns using loop in R

  29. 29

    Creating new objects using values ​in array using the for(of) function

HotTag

Archive