ReverseIterator class <see bottom>

K

Kabal

Anyone have a better way to implement the below code...


import java.util.Collection;
import java.util.NoSuchElementException;

public class ReverseIterator {
private Object[] elements = null;
private int n;

public ReverseIterator(Collection collection) {
elements = collection.toArray();
n = elements.length - 1;
}

public Object next() {
try {
return elements[n--];
} catch(IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}

public boolean hasNext() {
return (n >= 0);
}
}
 
R

Richard Chrenko

Anyone have a better way to implement the below code...


import java.util.Collection;
import java.util.NoSuchElementException;

public class ReverseIterator {
private Object[] elements = null;
private int n;

public ReverseIterator(Collection collection) {
elements = collection.toArray();
n = elements.length - 1;
}

public Object next() {
try {
return elements[n--];
} catch(IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}

public boolean hasNext() {
return (n >= 0);
}
}
If you can transform your Collection into a List...

LinkedList theList = new LinkedList();

// fill theList with elements HERE

if (!theList.isEmpty()) {
ListIterator theListIterator = theList.listIterator(theList.size());
boolean hasMoreElements = true;
while (hasMoreElements) {
theElement = theListIterator.previous();

// user code HERE

hasMoreElements = theListIterator.hasPrevious();
}
}
 
J

John C. Bollinger

Kabal said:
Anyone have a better way to implement the below code...

No, not really. I'd make it implement Iterator, myself, but for a
generic Collection

No, not really. I'd make it implement Iterator, myself, but I don't
know any better alternative to this general approach if you want to
handle generic Collections.


John Bollinger
(e-mail address removed)
import java.util.Collection;
import java.util.NoSuchElementException;

public class ReverseIterator {
private Object[] elements = null;
private int n;

public ReverseIterator(Collection collection) {
elements = collection.toArray();
n = elements.length - 1;
}

public Object next() {
try {
return elements[n--];
} catch(IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}

public boolean hasNext() {
return (n >= 0);
}
}
 
?

=?ISO-8859-1?Q?Thomas_Gagn=E9?=

Sure, write it in Smalltalk and send your collection #reverseDo:

Sorry about that. You probably wanted a Java solution.

Roedy Green had a good response to a similar question earlier this year
(in July according to google)

In general, here are three solutions to that problem.

1. extend the Collection class to provide a reverse iterator. See
http://mindprod.com/jgloss/iterator.html for how to write an Iterator.

2. export the data using toArray or with the iterator and get it into
an ordinary array. Then sort it with a custom Comparator that just
flipped the sign of the natural one, or by the negative of the
original index. Then, if you really have to, you can then turn the
sorted array back into an Iterator using the ArrayIterator code at
http://mindprod.com/jgloss/iterator.html

3. as you build the collection, build a parallel one, say a TreeSet
with a reverse Comparator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top