Easy question about collection

G

Gianni

I have a collection

Collection tabda;
while (tabda.hasMoreRows()) {

Iterator it_riga = tabdati.nextRow().iterator();
I read it with an iterator

while (it_riga.hasNext())
in this way I read all the collection .

My problem is that I have to read all the collection except the last row !!!
Any idea how can I do it?
thanks
Gianni
 
M

Michael Borgwardt

Gianni said:
I have a collection

Collection tabda;
while (tabda.hasMoreRows()) {

Iterator it_riga = tabdati.nextRow().iterator();
I read it with an iterator

while (it_riga.hasNext())
in this way I read all the collection .

My problem is that I have to read all the collection except the last row !!!
Any idea how can I do it?

The java.util.Collection interface does not have hasMoreRows() or nextRow()
methods, so what's this badly-named thing you're working with, and do
you want to read all its rows except the last or for each of the rows
(which might be real Collections) all its elements except the last (your
code suggests the latter)?

Anyway, after reach row or element, you cna test if there are more and
keep it only if there are.
 
P

Peter Kirk

Gianni said:
I have a collection

Collection tabda;
while (tabda.hasMoreRows()) {

Iterator it_riga = tabdati.nextRow().iterator();
I read it with an iterator

while (it_riga.hasNext())
in this way I read all the collection .

My problem is that I have to read all the collection except the last row !!!
Any idea how can I do it?
thanks
Gianni

While your iterator may have a notion of "order", I don't think that a
Collection in general has such a notion (although there are implementations
which do impose an order, of course).

So really it is not correct in general to think of a first or last item in a
Collection. (Be careful about your assumptions of order).

But maybe you could test "hasNext()" inside your loop. For example:

Collection c = new HashSet();

c.add( "one" );
c.add( "two" );
c.add( "three" );
c.add( "four" );

for ( Iterator it = c.iterator(); it.hasNext(); ) {
String s = (String)it.next();
if ( it.hasNext() ) {
System.out.println(s);
}
}



Peter
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top