Results 1 to 6 of 6
  1. #1
    BackY is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Accessing an ArrayList of another class

    Ok guys, this may've been posted and asked before, but it's so hard to explain in just a few words, so it's hard as hell to search for it as well!

    What I want to do is this, this is my first class:

    Java Code:
    public class Footballer {
        int goals;
        String surname= "";
        String team="";
        private static int counter=0;
        private int dres;
    }
    (this is just the header of the class, just to give you the idea of how it looks)...

    And this is my second class, which contains an ArrayList of the first class:
    Java Code:
    public class FootballTeam{
         String teamname="";
         String league="";
         ArrayList<Footballer>  f;
    }
    And this is my third class which contains an ArrayList of the second clas:
    Java Code:
    public class FootballLeague{
         String leaguename="";
         ArrayList<FootballTeam> ft;
    }
    What I want to do is, know how many of footballers are there in the league? Meaning how many of "f"s are in the "ft"... I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.

    I really hope I am clear enough, please ask me if you don't get what I'm asking..

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Accessing an ArrayList of another class

    To access a particular element in an ArrayList, use the get(int i) method. And ArrayLists also have a size() method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    BackY is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Re: Accessing an ArrayList of another class

    I'm trying this method to get the size of the array in the 2nd class, from the 3rd class (containing an ArrayList of classes of 2nd class, but no luck:

    Java Code:
    int counter=0;
                for(int i=0;i<this.ft.size();i++)
                {
                    counter+=this.ft[i].f.size();
                    
                 }
    I'm getting this: Array required, but ArrayList<FootballTeam> found ---

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Accessing an ArrayList of another class

    you need to do

    ft.get(i).f.size();

    ft[i] won't work for ArrayLists.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Accessing an ArrayList of another class

    Quote Originally Posted by BackY View Post
    I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.
    You either remember incorrectly or you made all the members in the C++ classes public, or you used the 'friend' construct in C++; except for the 'friend' feature, you can do the same in Java, but better create accessors in those classes ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    BackY is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Re: Accessing an ArrayList of another class

    thank you guys, I finaly managed to make it work..

Similar Threads

  1. Problem Accessing ArrayList in another class
    By MemberZero in forum New To Java
    Replies: 1
    Last Post: 10-27-2012, 11:19 PM
  2. Accessing Elements of a Reflected ArrayList
    By FALL3N1 in forum Advanced Java
    Replies: 3
    Last Post: 03-22-2012, 09:49 AM
  3. Accessing arrays from an ArrayList
    By Guy in forum New To Java
    Replies: 2
    Last Post: 01-02-2012, 07:19 PM
  4. Replies: 5
    Last Post: 01-18-2010, 07:00 AM
  5. Replies: 12
    Last Post: 11-05-2009, 07:12 AM

Posting Permissions

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