wood burning stoves 2.0*
The moose likes Java in General and the fly likes data structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "data structure" Watch "data structure" New topic
Author

data structure

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I would like to know if there is a little special data structure defined in API.
It should be something like photo watching under Windows system. Dealing with that structure is through 2 methods - next and previous, but if it is the last element and next() is invoked - the first element should be returned. And if it is the first element and previous() is invoked - the last element should be returned. So that's like circle. Is something like this defined in API?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Have you looked through the exiting Collection classes? Is there one that meets your requirements? If not, which one comes closest?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I think that it should be somehing like list. Its ListIterator has good methods but when it gets the last element it doesn't jump to the first one - no circle. Do I have to implement my specific collection?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

If the ListIterator's hasNext() method returns false, simply reset it to the start (or create a new one; that's faster). Likewise for hasPrevious().

Moving to Java in General.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Lukas:

This link might be useful to you.

John.
zahid zubair
Ranch Hand

Joined: Aug 29, 2009
Posts: 32
IF you want to go from last to first alone, then you can use this code:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

i will always be >= 0 and < listSize, so wouldn't the % listSize be useless?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Useful hint, zahid Zubair ( ). It works much faster with List implementations which also implement the RandomAccess tagging interface.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: data structure
 
Similar Threads
LinkedList
Queue collection
How to "page" results from a database?
Implementing Iterator problem - ConcurrentModificationException