• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Comparing to objects with ListIterator or Iterator

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

i have i wierd excercise in my course, i can not solve it by my self

i need to compare to objects in List with Iterator;
the objects have stating date and finishing dates;
i need to add objects that finishing for example in 01-april-2010 and starting in 02-april-2010 to one list

so  i think i know how i check iterator.next() objects, but how do i hace to compare it with next object??

this is code i need to make..



 
Bartender
Posts: 5061
188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ilya,

why not have TWO ListIterators? See the method List.listIterator. Take one that starts at index 0. and one that starts at index 1. Then you ccan do
 
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . why not have TWO ListIterators? . . .

But if you add objects to the List with one ListIterator, the other ListIterator will throw a concurrent modification exception.

IM: Does your line 9 work normally, or does it cause an exception to be thrown? Have you tried calling next() before the loop, in which case you will have a previous() element? Don't even start the loop for a List with < 2 elements. You won't need a loop for such a small List.

It seems a weird requirement to use such an implementation detail. Please ask for more explanation. Maybe it is special training in using Iterators.
 
Piet Souris
Bartender
Posts: 5061
188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:But if you add objects to the List with one ListIterator, the other ListIterator will throw a concurrent modification exception.


The elements are added to a different list.
 
Campbell Ritchie
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote: . . . The elements are added to a different list.

Thank you; I misunderstood that point
 
Piet Souris
Bartender
Posts: 5061
188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if you want to use only one iterator, this is also possible:
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic