• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Looping Thru an ArrayList

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, with these suggestions it is now working.
 
reply
    Bookmark Topic Watch Topic
  • New Topic