• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Accessing a collection of objects  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a university project using BlueJ. It involves creating
a GUI that displays nine buttons that each hold a value from 1 to 9. I
need to know how to iterate through the collection of buttons and
return their combined value
(45 in this case).

My array is called gameTile and its initialized as:
gameTile = new Tile[9];
for(int i = 0; i < 9; i++) {
gameTile[i] = new Tile(1+i);
}

When I click one of the nine buttons the value of that button becomes
zero, but if it is not clicked it should retain its value so when i go
through the array I want to add up all the button values that have not
been changed to zero.
My iterator looks like this

Private void totalUp {

Iterator<Tile> totalUp = gameTile[9].iterator ();
while(totalUp.hasNext()) {
System.out.println(totalUp.next(gameTile.value));

}

I appreciate any help in this matter. :-)
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Arrays don't have iterators. (And if they did, you would probably get it by calling iterator() on the array, gameTile.iterator() -- not on a Tile object in the array, gameTile[9].iterator().)

Instead, just iterate through the array elements using a loop -- the same way you did when instantiating the elements.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!