• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get index value from the Iterator  RSS feed

 
Amarendra Manchinasetty
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi ,

I have requirement that which requires the iterator to start looping from particular index. How can i accomplish that. Anybody please help me.Ex I have the actualPropertiesCount having 77 objects and i have inheritPropertiesCount as 74. I have to display only 75,76,77 object properties which are not inherit from the parent class obejct.

Here is the code i am working but its not giving the desire result. I want to display only last 3 values of the iterator values.


 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Don't use the == operator to test whether the contents of two String objects are the same. It doesn't do that, it only tests whether the two String objects are the same object. Use the equals() method instead, like this:

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amarendra Manchinasetty wrote:I have requirement that which requires the iterator to start looping from particular index. How can i accomplish that.

Use List.listIterator(int index). If you're not using a List then you should start using one, because Lists are the only collections with the notion of "index".

Paul Clapham wrote:

Slightly better, to prevent any possible NullPointerExceptions:
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the ListIterator interface there seems to be a curious omission, IMO.
It has a nextIndex() method.
It has a previousIndex() method.
It doesn't have a currentUndex() method.

I mean, yes I suppose you could do:

but it just seems a bit odd.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't previousIndex() return the same value after a call to next()? After all, calling next() and then previous() would return the ListIterator in the same state (unless either throws an exception).

So the following would be true:
1) listIterator.nextIndex() == X for some value of X
2) listIterator.next()
3) listIterator.nextIndex() == X + 1
4) listIterator.previousIndex() == X
5) listIterator.previous()
6) listIterator.nextIndex() == X, as the ListIterator is in the same state as in step 1.

So you either call nextIndex() before next(), or previousIndex() after next().
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!