• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with a For Loop

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is "results" ? You should check for the size of this "results" variable.
 
Nelly Verccety
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry, results is of type ArrayList.

So it is declared like this:

ArrayList results = DataAccess.executeQuery(theDBConnection, table,
fields, on, where, groupBy, orderBy);
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int end = results.size();
for (int i=0; i<end; ++i)
   ...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you could do :
for (int i=0; i< results.size(); i++)

This will loop until the end of the array.
 
Nelly Verccety
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sweet, I love you man.

Thanks ^^
 
reply
    Bookmark Topic Watch Topic
  • New Topic