Results 1 to 3 of 3
  1. #1
    new person is offline Member
    Join Date
    Nov 2008
    Posts
    6
    Rep Power
    0

    Default 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();

    Java Code:
    public void printAllMembers()
        {
            for(int i = 0; i < members.size(); i++)    
                {
                 System.out.println(members.toString()); 
                }
        }
    That is printing out all the members 6 times [That's the number of members]. I have tried things like:

    Java Code:
    System.out.println(members[i].toString());
    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?

    Thanks.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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
    It would appear that your using an ArrayList ,and they don't work the same way as arrays do:
    For example, to access ArrayList elements, you don't do the follwing to get an element:
    Java Code:
    myArrayList[i]
    For an ArrayList you do the following:
    Java Code:
    myArrayList.get(i)
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    ^ 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

  1. Swapping elements of an array help please
    By ikillu in forum New To Java
    Replies: 11
    Last Post: 01-15-2012, 08:49 PM
  2. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM
  3. Replies: 8
    Last Post: 08-27-2008, 10:45 PM
  4. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •