1 2 Previous Next 22 Replies Latest reply: Dec 21, 2008 10:41 AM by 807589 RSS

    Need a bit of help w/  ArrayLists

    800419
      Hi, I'm in an AP A Computer Science course, and we just finished learning about Arrays and ArrayLists. I'm trying to create a program that tracks, and saves the names of the players on the water polo team, along with some stats. I want to link the names and the stats w/ a player. I was thinking about creating something like this:
      public Player(String nm, int a)
      {
            // Integer a corresponds to a spot in an Array containing stats
            // nm corresponds to the name of the player
      }
      OR
      public Player(String nm, ArrayList<int> a)
      {
            // ArrayList a corresponds to an array of stats
            // nm corresponds to the name of the player
      }
      then i would have an array or ArrayList of players, and a separate array of stats for each player, and then link the two. Is there a way to do it?

      Edited by: Supp4sk1llz on Dec 21, 2008 8:23 AM
        • 1. Re: Need a bit of help w/  ArrayLists
          800313
          I dont really understand your question, why do you need to link the two constructors? Constructor 2 should be fine


          then to get a specific stat from a player just use
          myPlayer.returnStat(5);
          
          //put this method in Player class
          public int returnStat(int s){
             return  this.myArraylist.get(s)
          }
          • 2. Re: Need a bit of help w/  ArrayLists
            800419
            No i was wondering if there was a way to link 2 ArrayLists

            Like so:
            ArrayList<Player> players = new ArrayList<Player>();
            
            ArrayList<Stats> stats = new ArrayList<Stats>();
            
            // I need a way to correspond each player to their stats
            Edited by: Supp4sk1llz on Dec 21, 2008 8:34 AM
            • 3. Re: Need a bit of help w/  ArrayLists
              800445
              I think you're looking for a HashMap .
              • 4. Re: Need a bit of help w/  ArrayLists
                807589
                I'm trying to create a program that tracks, and saves the names of the players on the water polo team, along with some stats. I want to link the names and the stats w/ a player.
                Well, you should think in Object Oriented way....and that means a 'Player' class with attributes like 'name', 'stats', etc. would be ideal. Then you can make either array or array list to keep record of the whole team.

                Make a class like:
                class Player
                {
                 String name;
                 //variables to hold stats of the players
                
                 //Constructors
                
                 //getters and setters
                
                 //other methods
                }
                then to hold whole team do this:
                Player[] team=new Player[11];    //though I don't know how many players are there in water polo team....
                or:
                ArrayList<Player> team=new ArrayList<Player>(11);   
                Thanks!

                Edit: When I started to type, there was no reply, but.....

                Edited by: T.B.M on Dec 21, 2008 10:10 PM
                • 5. Re: Need a bit of help w/  ArrayLists
                  800313
                  You should have a class called Player, when you make a player you should set their stats, name etc

                  each time you make a player add him/her to your arraylist called "players"

                  to get a particular stat from a person
                  Player temp = players.get(0;    //gets player1
                  ArrayList<Stats> tempstats  = temp.get(0);   //return first Stats object
                  int aStat = tempstats.get(0);                  //returns first stat in your Stat object
                  Calypso
                  • 6. Re: Need a bit of help w/  ArrayLists
                    800419
                    import java.util.*;
                    import java.util.ArrayList;
                    
                    public class Player
                    {
                        public String name;
                        public int statSpot;
                    
                        public Player(String nm, int ArraySpot)
                        {
                            name = nm;
                            statSpot = ArraySpot;
                        }
                        
                        public String getName()
                        {
                            return name;
                        }
                        
                        public int getStatPosition()
                        {
                            return statSpot;
                        }
                        
                        public String toString()
                        {
                            String str;
                            str = "The players name is: " + name + " and his stats are stored in"
                                    + " spot # " + statSpot + "of the ArrayList";
                            return str;
                        }
                                    
                    }
                    This is what i have so far for my player class. Also, I do not know what hash maps are because we haven't learned them yet.

                    So, is there a way to link 2 ArrayLists, or is that something more advanced that i will have to wait for?

                    Edited by: Supp4sk1llz on Dec 21, 2008 8:42 AM
                    • 7. Re: Need a bit of help w/  ArrayLists
                      807589
                       public String toString()
                      {
                      String str;
                      str = "The players name is: " + name + " and his stats are stored in"
                      + " spot # " + statSpot + "of the ArrayList";
                      return str;
                      }
                      Why "his stats are stored at some spot in the array list"?? Can't they be in Player class itself as its attributes?

                      Thanks!
                      • 8. Re: Need a bit of help w/  ArrayLists
                        800419
                        I don't quite get what you are saying...

                        I have a player class, and a stat class...or is that 2 much?

                        can some1 please elaborate?
                        • 9. Re: Need a bit of help w/  ArrayLists
                          800313
                          what he is saying i think is you do not need "statspot"
                          instead each player object should just contain an arraylist of stats and a name ie your constructor should look like this
                          public Player(String Name, ArraList<Stats> stats){
                                 this.Name = Name;
                                 this.stats = stats;
                          
                          }
                          • 10. Re: Need a bit of help w/  ArrayLists
                            800419
                            Ok i get it now, but i still need a stat class...correct?
                            • 11. Re: Need a bit of help w/  ArrayLists
                              800313
                              what is a "stat" what attributes does it have
                              • 12. Re: Need a bit of help w/  ArrayLists
                                800419
                                import java.util.ArrayList;
                                import java.util.*;
                                
                                public class Stats
                                {
                                    public int shots; 
                                    public int goals;
                                    public double perMade;
                                    public int gamesPlayed;
                                    public int saves;
                                    
                                    public Stats(int s, int g, int gp, int sa)
                                    {
                                        shots = s;
                                        goals = g;
                                        gamesPlayed = gp;
                                        saves = sa;
                                    }
                                    
                                    public int getShots()
                                    {
                                        return shots;
                                    }
                                    
                                    public int getGoals()
                                    {
                                        return goals;
                                    }
                                    
                                    public double getPerMade()
                                    {
                                        perMade = this.getGoals()/this.getShots();
                                        return perMade;
                                    }
                                    
                                    public int getGamesPlayed()
                                    {
                                        return gamesPlayed;
                                    }
                                    
                                    public int getSaves()
                                    {
                                        return saves;
                                    }
                                    
                                    public String toString()
                                    {
                                        String str;
                                        str = "Player has " + this.getShots() + " and " + this.getGoals() + " goals"
                                              + "\n He has made " + this.getPerMade() + "% of his shots"
                                              + "\n He has also played in" + this.getGamesPlayed() + " games";
                                        
                                        return str;
                                    }
                                }
                                this is my stat class...
                                • 13. Re: Need a bit of help w/  ArrayLists
                                  807589
                                  I don't quite get what you are saying...
                                  Perhaps , misunderstanding is on my side, I don't know if 'stats' mean something different in case of water polo. As I said, I don't know about water polo ;-)
                                  But if 'stats' mean statistics of the player then they can be made attributes..... I know few sports like cricket, football, tennis,etc.
                                  Lets take case of Cricket (because that I know the best)....
                                  then major statistics of player can be
                                  ---Matches Played
                                  ---Innings
                                  ---Runs scored
                                  ---centuries
                                  ---half centuries
                                  ---Average
                                  etc.

                                  then I would make the class like:
                                  class Player{
                                   String name;
                                   //stats
                                   int matchesPlayed;
                                   int innings;
                                   int runs
                                   int centuries, halfCenturies;
                                   //Contructors
                                   .....
                                   }
                                  I thought that was the meaning of stats.

                                  Thanks!

                                  Edit1: Sorry, late again.... and the thread has changed its complexion... :-)

                                  Edited by: T.B.M on Dec 21, 2008 10:32 PM

                                  Edit2: As you have 'Stat' class then make one attribute of 'Stat' type in 'Player' class....wouldn't that be the solution?

                                  Edited by: T.B.M on Dec 21, 2008 10:35 PM
                                  • 14. Re: Need a bit of help w/  ArrayLists
                                    800419
                                    I know what you are saying and ur not late, u helped me come up w/ a breakthrough.

                                    I will just make a player class, with all the attributes built in...easy enough
                                    1 2 Previous Next