aspose file tools*
The moose likes Beginning Java and the fly likes Need help with a For Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with a For Loop" Watch "Need help with a For Loop" New topic
Author

Need help with a For Loop

Nelly Verccety
Greenhorn

Joined: Feb 16, 2006
Posts: 8
This is my for loop:
for (int i=0; i< 10; i++) {
ArrayList row = (ArrayList) results.get(i);
Double amount_due = (Double) row.get(0);
Double amount_received = (Double) row.get(1);
System.out.println("Amount due = " + amount_due);
}

I don't get any error, what I want is a better way to end the loop. Because it says to end when it is less than 10. I want to end when the array actually ends. Because I know that there are 10 results, but I know there is a better way to end the loop. Like a method or something, a more professional way to end it, what if there are millions of values?

Thanks in advance...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What is "results" ? You should check for the size of this "results" variable.


[My Blog]
All roads lead to JavaRanch
Nelly Verccety
Greenhorn

Joined: Feb 16, 2006
Posts: 8
Oh sorry, results is of type ArrayList.

So it is declared like this:

ArrayList results = DataAccess.executeQuery(theDBConnection, table,
fields, on, where, groupBy, orderBy);
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

int end = results.size();
for (int i=0; i<end; ++i)
   ...


[Jess in Action][AskingGoodQuestions]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

So you could do :
for (int i=0; i< results.size(); i++)

This will loop until the end of the array.
Nelly Verccety
Greenhorn

Joined: Feb 16, 2006
Posts: 8
Sweet, I love you man.

Thanks ^^
 
 
subject: Need help with a For Loop