« Return to Thread: List.contains problem

Re: List.contains problem

by Rick Fincher :: Rate this Message:

Reply to Author | View in Thread

Hi Jimmy,

The contains() method just loops through the list and checks to see if
any of the objects in the list are the same object as the one you pass
in through the contains() method.

By "the same" I mean actually the same, not just one that has the same
entries for label and value, but physically the very same option object.

If you are trying to see if an option with the same label and same value
is already in there, you can get an iterator and loop through quickly,
as in:

ArrayList<Option> opt = new ArrayList<Option>();
       
        Iterator<Option> optIt = opt.iterator();
        boolean duplicateFound = false;
       
        while (optIt.hasNext()) {
            if ( (optIt.next().getValue().toString().equals
(testOpt.value()).toString()) &&  
(optIt.next().getLabel().equals(testOpt.label())) ) {
                duplicateFound = true;
            }
        }

You can make that a method that returns a boolean to make it a little
cleaner if you like.  This is assuming that the toString() method for
the value is meaningful.  If the value is something else, ubstitute the
appropriate comparison.

The label is already a String so you don't have to use toString().


Rick

Jimmy200012 wrote:

> Rick,
>
> Thank you so much for the quick reply. So, is there any quick way to check
> if a new Option is in the Option Array []? Do I have to loop through to find
> out? So what's the contians method for?
>
> Thanks again!
> Jimmy
>
>
> Rick Fincher wrote:
>  
>> Hi Jimmy,
>>
>> This does not work because testOpt and MyOpt[0] are two different
>> objects, even though their labels and values are the same.  If your code
>> was the following it would work:
>>
>> Option testOpt = new Option("R2", "R2");
>>
>> Option[] myOpt = new Option[]{
>>             testOpt,
>>             new Option("L22", "L2")
>>         };
>>
>> The equals(0 method compares pointers to the object.  For Strings this
>> works because Strings are immutable values and only one copy of a String
>> containing the same characters is stored in memory.
>>
>> If you do code like this:
>>
>> String s1="hello";
>> String s2="hello";
>>
>> Only one copy of the String "hello" is stored in memory with both s1 and
>> s2 pointing to it.  If you do this later:
>>
>> s2="goodby";
>>
>> A new String "goodby" is created and s2 is set to point to it, unless
>> "goodby" was already in memory, then s2 would be set to point to the
>> pre-existing String "goodby".
>>
>> Because of that the String method equals() will work in situations like
>> this.
>>
>> That's one of the reasons String methods are slow in Java.  Every time you
>> change it a new String is created and the old one is left for garbage
>> collection.
>>
>> Manipulating StringBuffers is much faster and more memory efficient.
>>
>> Rick
>>
>>
>> Jimmy200012 wrote:
>>    
>>> Hi, need some help to understand why this won't work:
>>>
>>> import com.sun.webui.jsf.model.Option;
>>> import java.util.*;
>>>
>>> public class ArrayListContains {
>>>
>>>     /** Create a new instance of ArrayListContains */
>>>     public ArrayListContains() {
>>>     }
>>>
>>>     public static void main(String[] args) {
>>>         // Now the real test;
>>>         Option[] myOpt = new Option[]{
>>>             new Option("R2", "R2"),
>>>             new Option("L22", "L2")
>>>         };
>>>
>>>         Option testOpt = new Option("R2", "R2");
>>>
>>>         ArrayList<Option> optList = new
>>> ArrayList<Option>(Arrays.asList(myOpt));
>>>         if (optList.contains(testOpt)) {
>>>
>>>             // YOU THOUGHT WOULD PRINT THIS, BUT NOT.
>>>             // com.sun.webui.jsf.model.Option DID NOT IMPLEMENTS EQUALS?
>>>             // WHERE CAN I FIND SOME DOC ABOUT THIS?
>>>             System.out.println("Yes!!!!!!!!!!!!R2");
>>>         } else {
>>>             System.out.println("why?????????????");
>>>         }
>>>     }
>>> }
>>>  
>>>      
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@...
>> For additional commands, e-mail: users-help@...
>>
>>
>>
>>    
>
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

 « Return to Thread: List.contains problem