Hi

I would think that using a TreeSet instead of ArrayList would be the right
way to do this. But if you have to to use ArrayList, then you need to
override the equals() method in the class Words.  The ArrayList does not
know how to compare two objects of the Class Words...

NetBeans has a way to aid you in this, just place the cursor at the bottom
of the Class Words, before the last } then do Alt-Insert and select
"equals() and hashcode() ... and Netbeans will create those methods for
you...!
best regards.
  - Halli
2009/9/19 Ishtiaq Ahmed <[email protected]>

>   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
-~----------~----~----~----~------~----~------~--~---

Reply via email to