This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Well, you're not returning the type you say you are. And you're checking every item in the list you just created against the list you just created, so every element is pretty much guaranteed to be in the list... because you put it there. And if it's not there (impossible), you put it back into the same list.
I suspect what you meant to do was check every item in the list you pass in (which you don't do) against the return list--maybe head down that road and post a followup with your new code.
Can I suggest that you check if the element is in the list before you add it? Collections have a contains method which returns a boolean as to whether an element exists. Can I suggest you use the contains method to decide whether or not an item is already in the list and thus whether or not you have to add the item..
Rajendra Prakash wrote:Correct this code to remove duplicates in arraylist1
After printing 'al2' we get a list with duplicates .... wat will be the problem i detected tat problem is with if(!al2.equals(al1)) statement but i could not recognize wats d problem .. if v remove
the not symbol '!' then the control isn't not coming inside the for loop itself