ArrayList Problems...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShaveDave27
    New Member
    • Apr 2007
    • 22

    ArrayList Problems...

    Hi, I'm trying to create an Arraylist Class which will allow me to output the whole list in one method. I think i've created the arraylist correctly but when i try to get the list to output i come across problems.
    Here is my code,
    Code:
    import java.util.*;
    import java.util.ArrayList.*;
    
    public class People
    {
       
        public static void main (String[] args)
        {
            Person Chris = new Person("Chris", "Davies", 10, 7);
            Person Jess = new Person ("Jess", "Levy", 4, 17);
            Person Koo = new Person ("Tom", "McHowat", 4, 14);
            Person Andy = new Person ("Andy", "Davies", 3, 6);
            
             ArrayList<Person> people = new ArrayList<Person>();
            people.add(Chris);
            people.add(Jess );
            people.add(Koo );
            people.add( Andy );
            
             int i = people.size();
            Person allPeople = people.get(i-1);
        }
               
        }
    I've also tried to put the last two statements in a seperate method but the compiler can't find the variable people when i do this.

    Any help would be much appreciated!
    Thanks

    Dave
    Last edited by JosAH; Apr 29 '07, 03:35 PM. Reason: added [code] ...[/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    I don't understand what you're doing in those last few statements: to me it
    looks as you're getting the last Person from your "people" list; i.e. you're
    not printing anything.

    Hava you tried this?
    Code:
    System.out.println("people: "+people);
    What does it print?

    kind regards,

    Jos

    Comment

    • ShaveDave27
      New Member
      • Apr 2007
      • 22

      #3
      Originally posted by JosAH
      I don't understand what you're doing in those last few statements: to me it
      looks as you're getting the last Person from your "people" list; i.e. you're
      not printing anything.

      Hava you tried this?
      Code:
      System.out.println("people: "+people);
      What does it print?

      kind regards,

      Jos
      Hi, Thanks again!

      I wasn't sure what i was trying but found some information on the web and realised i was calling the people list rather than my person class. I've now changed it and it will allow me to print any of my methods from the person class! I've eventually got it!

      Thanks for your help

      Dave

      Comment

      Working...