• 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

Calling methods in a loop

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem. If i have to call a bunch of methods with similar names like

String rec1 = Records.getRecord1();
String rec2 = Records.getRecord2();
String rec3 = Records.getRecord3();

is there a way I can do this in a loop?

for example:

for(int count=1;count<4;count++){

rec//count// = Records.getRecord//count//();
}
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, not if the records are named getRecordN (where N is 1, 2, 3, ...). (Well, you can do it via reflection, but...).

Note that the design of the Records class should have been done differently: Instead of a whole bunch of getRecordN methods, there should have been just one getRecord(int number) method; then it would be easy:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(Well, you can do it via reflection, but...).



Okay, I'm bored. Let's do this with reflection. I guess if you have this...



You can do this...



Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic