Dear all,
I have a problem with ArrayList handling. Suppose i am going to make a
ArrayList which contains string token. But i dont want to add same string
token, that i previously inserted. How can i manage it? In my code i use
Collections.contains() method. But it does not work.
Here is my code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.StringTokenizer;
/**
*
* @author ishtiaq
*/
class Words implements Comparable{
String wordName;
Words(String word) {
wordName = word;
}
public int compareTo(Object o) {
Words w = (Words) o;
return wordName.compareTo(w.wordName);
}
}
public class P10815 {
public static String freshCopy(String word){
word = word.toLowerCase(Locale.FRENCH);
String ret = "";
for(int i = 0; i < word.length(); i++)
if(Character.isLetter(word.charAt(i)))
ret += word.charAt(i);
return ret;
}
public static void main(String args[]){
ArrayList<Words> list = new ArrayList<Words>();
String[] buffer = new String[6];
buffer[0] = "Adventures in Adventures Disneyland";
buffer[1] = "";
buffer[2] = "Two blondes were going to Disneyland when they came to a
fork in the";
buffer[3] = "road. The sign read: \"Disneyland Left.\"";
buffer[4] = "";
buffer[5] = "So they went home.";
for(int i = 0; i < buffer.length; i++){
StringTokenizer token = new StringTokenizer(buffer[i]);
while(token.hasMoreElements()){
String word = (String) token.nextElement();
word = freshCopy(word);
if(list.contains(new Words(word)))
continue;
else list.add(new Words(word));
}
}
Collections.sort(list);
for(Words wd : list) System.out.println(wd.wordName);
}
}
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---