Printing Array elements
Note: I'm using BlueJ
Trying to answer this question:
"In class Club write a new method printAllMembers(). This should use a for-each loop to access each Membership object in turn. Print individual member's records using the toString() method. Add a call to this method in the demo() method in ClubDemo"
This is the demo() method which is adding people to the Membership:
public void demo()
club.join(new Membership("David", 2, 2004, 5));
club.join(new Membership("Michael", 1, 2004, 3));
club.join(new Membership("Conn", 1, 2004, 2));
club.join(new Membership("Daffy", 7, 2003, 1));
club.join(new Membership("Lloyd", 3, 2004, 6));
club.join(new Membership("Rosie", 1, 2004, 4));
System.out.println("The club has " +
And this is as far as i've got on the printAllMembers();
That is printing out all the members 6 times [That's the number of members]. I have tried things like:
public void printAllMembers()
for(int i = 0; i < members.size(); i++)
But it just errors telling me:
array required, but java.util.ArrayList<Membership> found
How can i make it access the specifc element it needs?
I don't think we have the complete picture here (at least i don't).
It would appear that your using an ArrayList ,and they don't work the same way as arrays do:
- What is members?
- What is Membership? I'm assuming that this is an ArrayList
For example, to access ArrayList elements, you don't do the follwing to get an element:
For an ArrayList you do the following:
^ what he said. check out the arraylist api here for more info: ArrayList (Java 2 Platform SE v1.4.2)
you might find more useful things that you'll slap yourself for trying to implement on your own. api's are your best friend in java (yet also your worst enemy).