Hi Sarath, First u should know two things. 1. Content Comparison. 2. Reference Comparision So here in if(list.equals("3")) u r using equals method. In the list object, it holds multiple objects which are defined as string, as placed the content in " double quotes". So it treats as object. When u r saying, list.equals("3") , u r comparing("3") a string object, with the list object which hold multiple string object. so when it encountere "3" in the list object. The control will come to remove method. so it is removing 3,(which is treated as string object) from the list. Here It is reflexive: for any non-null reference value x, x.equals(x) should return true. As the encountered values is matching "3", it removed from the list object. So that's y output 1,2,4,5. So it is doing content comparision. Reason y i said to go thru with diff of 1.Content Comparision 2. Reference Comparision, u will get clear idea, when u r using Hashmap n WeakHashmap etc. equals method is defined in java.lang package. If any help required kindly let me know. Thanks & Regards JhonShravan | 0 |
By: [email protected] On: Fri Sep 06 07:08:51 EDT 2013 0 0 0 | 0 |
Are You Satisfied :2Yes3No |
@jhonshravan : the above program prints 1,2,3,4,5. you are explaining why it prints 1,2,4,5. ???? if(list.equals("3")){ list.remove("3"); // this line never executes }+ | 0 |
By: [email protected] On: Tue Sep 10 22:58:16 EDT 2013 0 0 0 | 0 |
Are You Satisfied :3Yes1No |
No need to iterate for your desired result...just write the following. List list=new ArrayList(); if(list.contains("3")){ list.remove("3"); } | 0 |
By: [email protected] On: Tue Sep 17 03:13:36 EDT 2013 5 20 0 | 25 |
Are You Satisfied :1Yes0No |