• 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

Remove dates from an array list using iterator

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to modify the ArrayListofDates so that after generating the random dates it loops thru the Dates and checks to see if it is already in the arraylist. If there is a duplicate it then should prompt the user if they want to remove that date from the array. I am trying to use the contains and remove method of array list. Here is the code and it compiles fine but does not work as intended.



Any suggestions would be greatly appreciated.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Josiah,

In the code that you have posted I couldn't see where you were using "contains" but I see a set.add(element) function. Either ways, since you are referring to a list of Dates, so you would have to override and write your own contains definition.

Regards,
Praveen.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad! This is the actual documentation for contains method

boolean contains(Object o) -
"Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))."

As you can see, the contains will return true if and only if the "equals" function returns true(assuming you have atleast 1 element in the list). But by-default the equals method here refers to the equals definition of Object class.

So please ignore the suggestion in the previous post, you would have to override the equals method instead(to compare 2 Dates).

 
Josiah smith
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have any examples of code that uses iterators and the contains method. The code I created works for integers within an array, but when I make the array contain dates it doesn't produce the correct output.

Thanks for your help
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, basic datatypes like Integers and Strings have their own version of equals method, so you can use a contains function on these types of lists directly.

I'll give you an example, see if you can write the actual code.



After you write the equals logic according to your requirements, subsequent calls to contains method of a CustomDate list would further call this equals method.

Note - If you override equals method for a class, you need to override another default method called hashCode. Do read up more on this before you do the override.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar M K wrote:. . . basic datatypes like Integers and Strings . . .

Note - If you override equals method for a class, you need to override another default method called hashCode. Do read up more on this before you do the override.

Why are you calling Integer and String basic? There is nothing basic about them, as if they were primitives.

Please don’t double-space code in the code tags, nor write line numbers, which are added automatically.
Beware of dates like 8 2 2012. If the month is 2, that counts as March in the Calendar class. I would have preferred to get a daysInMonth value from the switch block and use the random call once only.

And welcome to the Ranch
 
Josiah smith
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Sorry If i double spaced i have been working on this for days. This is what I am coming up with to detect and prompt removal for the duplicate dates. I am having a rough time though. I think my error lies in the condional statement. I have been working on this for days and nobody has really been able to help me. Thank you very much for your time.
V/r,
Josiah
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Josiah smith wrote:Hello,
Sorry If i double spaced i have been working on this for days. This is what I am coming up with to detect and prompt removal for the duplicate dates. I am having a rough time though. I think my error lies in the condional statement. I have been working on this for days and nobody has really been able to help me. Thank you very much for your time.
V/r,
Josiah



I found your original way of using a Set quite ok.

I'd suggest using a slight trick with the dates however: you can get the date "value" as a long with the date.getTime() method. Now just define your set to contain Long (note the capical 'L') elements. Then you can simple write something like


EDIT: an addition to your original post. You are aware that you are running through the iterator already once, before you do your actual "test and remove" run?

EDIT2: and make sure to define the set outside of the while loop!
reply
    Bookmark Topic Watch Topic
  • New Topic