Results 1 to 3 of 3
Thread: Printing Array elements
- 02-23-2009, 08:20 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 6
- Rep Power
- 0
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:
Java Code: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 " + club.numberOfMembers() + " members."); club.printAllMembers(); }
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:Java Code:public void printAllMembers() { for(int i = 0; i < members.size(); i++) { System.out.println(members.toString()); } }
But it just errors telling me:Java Code:System.out.println(members[i].toString());
array required, but java.util.ArrayList<Membership> found
How can i make it access the specifc element it needs?
Thanks.
- 02-23-2009, 08:51 PM #2
Array? ArrayList?
I don't think we have the complete picture here (at least i don't).
- 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:Java Code:myArrayList[i]
Luck,Java Code:myArrayList.get(i)
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-23-2009, 08:57 PM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
^ 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).
Similar Threads
-
Swapping elements of an array help please
By ikillu in forum New To JavaReplies: 11Last Post: 01-15-2012, 08:49 PM -
Help printing specific ArrayList elements
By CirKuT in forum New To JavaReplies: 5Last Post: 02-03-2009, 12:24 AM -
How to check whether two elements are available in an array?
By venkatteshb in forum New To JavaReplies: 8Last Post: 08-27-2008, 10:45 PM -
reference to elements in array
By Igor in forum New To JavaReplies: 1Last Post: 12-14-2007, 11:56 AM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks