I'm trying to get all possible word couples from a sentence and store it to an ArrayList
where MyClass.java
class MyClass{
private static ArrayList<String> wordList;
private static ArrayList<Integer> wordListCode;
private static int frequency;
private static int timeId;
public MyClass(ArrayList<String> words, int tid, int freq){
wordList = new ArrayList<String>();
wordListCode = new ArrayList<Integer>();
if(words.get(0).hashCode()> words.get(1).hashCode()){
wordList.add(words.get(0)); wordListCode.add(words.get(0).hashCode());
wordList.add(words.get(1)); wordListCode.add(words.get(1).hashCode());
}else{
wordList.add(words.get(1)); wordListCode.add(words.get(1).hashCode());
wordList.add(words.get(0)); wordListCode.add(words.get(0).hashCode());
}
frequency = freq;
timeId = tid;
}}
My problem is that when I'm trying to add an MyClass object to the ArrayList, adds one node but replaces all the other nodes with the values of the last one...
Here is some code:
ArrayList<MyClass> couples = new ArrayList<MyClass>();
ArrayList<String[]> couplesList = new ArrayList<String[]>();
couplesList = getWordCouplesList("demanding forensic technical comms systems");
for(int o=0; o<couplesList.size(); o++){
String word1, word2;
if(couplesList.get(o)[0].hashCode()> couplesList.get(o)[1].hashCode() ){
word1 = couplesList.get(o)[0];
word2 = couplesList.get(o)[1];
}else{
word1 = couplesList.get(o)[1];
word2 = couplesList.get(o)[0];
}
ArrayList<String> items = new ArrayList<String>(Arrays.asList(couplesList.get(o)));
MyClass temp = new MyClass((ArrayList)items, 1, 1);
couples.add( temp);
}
for(int i=0; i<couples.size(); i++){
System.out.println((i+1)+"couple = "+couples.get(i).getWordList().get(0)+" , "+couples.get(i).getWordList().get(1));
}
This gives output :
1couple = comms , systems
2couple = comms , systems
3couple = comms , systems
4couple = comms , systems
5couple = comms , systems
6couple = comms , systems
7couple = comms , systems
8couple = comms , systems
9couple = comms , systems
10couple = comms , systems