Fryguy8
09-04-2004, 09:16 PM
I'm writing a link validator, and one of the requirements is for it to not check the same link 2x. I thought about this for a while, and I think the easiest way to do this is to add the URLs to a linked list, and then later on run a removeDupes function to do it's namesake.
First of all, if you have a better idea, I'm open to suggestions :)
So I implemented the linked list, works great, I can traverse the link and stuff fine, however my removeDupes is hacked together and not working, and I'm looking for some input to clean up the code:
public static void removeDupes(LinkedList urlList)
{
URL firstCompare, secondCompare;
int listSize = urlList.size();
ListIterator firstIterator = urlList.listIterator(0);
int counter = 0;
while (firstIterator.hasNext())
{
firstCompare = (URL) firstIterator.next();
counter++;
if (firstIterator.hasNext())
{
ListIterator secondIterator = urlList.listIterator(counter);
while (secondIterator.hasNext())
{
secondCompare = (URL) secondIterator.next();
if (firstCompare.sameFile(secondCompare))
secondIterator.remove();
}
}
}
Assume the comparisons are working correctly, and the List is properly generated (it is). The problem occurs when a duplicate actually gets removed. counter gets screwed up and I think it tries to traverse an item in the list that isn't there, here's the error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodificatio n(Unknown Source)
at java.util.LinkedList$ListItr.next(Unknown Source)
at LinkValidator.removeDupes(LinkValidator.java:151)
at LinkValidator.main(LinkValidator.java:67)
If anyone has an easy fix for this, an easier way to write removeDupes, or a better data structure to use, I'm up for suggestions
First of all, if you have a better idea, I'm open to suggestions :)
So I implemented the linked list, works great, I can traverse the link and stuff fine, however my removeDupes is hacked together and not working, and I'm looking for some input to clean up the code:
public static void removeDupes(LinkedList urlList)
{
URL firstCompare, secondCompare;
int listSize = urlList.size();
ListIterator firstIterator = urlList.listIterator(0);
int counter = 0;
while (firstIterator.hasNext())
{
firstCompare = (URL) firstIterator.next();
counter++;
if (firstIterator.hasNext())
{
ListIterator secondIterator = urlList.listIterator(counter);
while (secondIterator.hasNext())
{
secondCompare = (URL) secondIterator.next();
if (firstCompare.sameFile(secondCompare))
secondIterator.remove();
}
}
}
Assume the comparisons are working correctly, and the List is properly generated (it is). The problem occurs when a duplicate actually gets removed. counter gets screwed up and I think it tries to traverse an item in the list that isn't there, here's the error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodificatio n(Unknown Source)
at java.util.LinkedList$ListItr.next(Unknown Source)
at LinkValidator.removeDupes(LinkValidator.java:151)
at LinkValidator.main(LinkValidator.java:67)
If anyone has an easy fix for this, an easier way to write removeDupes, or a better data structure to use, I'm up for suggestions