• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looping Thru an ArrayList  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get the following to loop thru an ArrayList and add new items that do not exists but no elements are being added.



Why is it that after you stare at code for so long you become blind. This would not work for me because I had it in the wrong connection class.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect it to throw ConcurrentModificationException, although simply not behaving as expected is also a possibility.

The only legal way to modify a Collection that's under iteration is through the Iterator. Iterator has a remove() method, and ListIterator() has and add() method. So you'd have to use the ListIterator directly, rather than the foreach loop, or else do the add outside the iteration.

Your logic doesn't make sense though. That logic doesn't say, "Add this element if it's not already in the list." It says, "For every element in this list that's not this object, add it." So if you have 10 elements, and one of them is the one you're searching for, and 9 of them are different, you'll be adding that value 9 times, even though it's already there.

Also, unless you have local variables with the same names, you don't need all those "this"es. They're just clutter.
 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:That logic doesn't say, "Add this element if it's not already in the list." It says, "For every element in this list that's not this object, add it."


The code I have does not do anything. It is like it never fires . But you are correct in what I am trying to accomplish.

I want to create an array while looping thru my data set that has only unique location field data.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:
Jeff Verdegan wrote:That logic doesn't say, "Add this element if it's not already in the list." It says, "For every element in this list that's not this object, add it."


The code I have does not do anything. It is like it never fires .


Nevertheless, the flaws I pointed out need to get fixed.

If that code is not being executed, then something that leads up to it is buggy. Looking at code that we never get to isn't going to help.



I want to create an array while looping thru my data set that has only unique location field data.


So you have some data set, and that data set may have duplicates. You want to copy that data set into an array, but without any duplictes. Yes?

The easiest way would be to put the data set into a java.util.Set, as they prevent duplicates, and then just copy the contents of that Set to your array. This way the API does the work of filtering out dupes for you.

If you can't or won't do it that way, then your code would need to look something like this:


 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, with these suggestions it is now working.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!