• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simplifying a list of assignments  RSS feed

 
Dan Grindstaff
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have the following code where I create an object each time and reuse alot of code:


I believe there has to be a cleaner way of implementing this and I am thinking of perhaps creating an array of strings of the names and looping through, creating a field object at each iteration and storing it in another array. Any suggestions are greatly appreciated.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this maybe?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I'd do - create one (static final) String[] with the labels, then iterate over this array, creating and adding fields as required.
 
Dan Grindstaff
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect! Thank you!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!