This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Using Iterator with LinkedList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Iterator with LinkedList" Watch "Using Iterator with LinkedList" New topic

Using Iterator with LinkedList

Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
I'm trying to use an iterator in a method to count the number of duplicates in a list of integers. I have a logic problem in it somewhere because it only counts one set of duplicates every time even if there's more than one. Here's the code:

Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Just figured it out:
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Also, the list must be sorted for this method to work. I used Collections.sort(list); before passing the list to the method.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19557

You'd better not use == comparison even for Integers. Use equals() instead, and beware of null values.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Using Iterator with LinkedList
Similar Threads
What is an assignment?
ArrayList with Iterator
Removing dups from array
removing dups using Set
Eliminating Duplicate in Arraylist and moving the dups to another arraylist.