Results 1 to 2 of 2
- 12-28-2010, 07:30 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
Java Arraylist (.get from other class)
I have these two classes:
I'd like to get two teams from the main class' arraylist using the get method of the arraylist, but for some reason Teams cannot be seen. I'm pretty new to Java, and this is perhaps my 3rd or 4rth coding project. Can anyone help me find a solution? Your help and time is very much appreciated!Java Code:import java.util.ArrayList; public class FIFA { public static void main (String[] args) { ArrayList<Team> Teams = new ArrayList<Team>(); Team Algeria = new Team("Algeria", 1.7, 0); Team Argentina = new Team("Argentina", 4.6, 1); Team Australia = new Team("Australia", 3.7, 2); Team Brazil = new Team("Brazil", 4.7, 3); Team Cameroon = new Team("Cameroon", 3.8, 4); Team Chile = new Team("Chile", 3.9, 5); Team IvoryCoast = new Team("Côte d'Ivoire", 4.3, 6); Team Denmark = new Team("Denmark", 3.6, 7); Team England = new Team("England", 4.8, 8); Team France = new Team("France", 4.6, 9); Team Germany = new Team("Germany", 4.7, 10); Team Ghana = new Team("Ghana", 3.3, 11); Team Greece = new Team("Greece", 2.7, 12); Team Honduras = new Team("Honduras", 1.3, 13); Team Italy = new Team("Italy", 4.6, 14); Team Japan = new Team("Japan", 4.6, 15); Team KoreaDPR = new Team("Korea DPR", .7, 16); Team KoreaRepublic = new Team("Korea Republic", .9, 17); Team Mexico = new Team("Mexico", 3.1, 18); Team Netherlands = new Team("Netherlands", 4.6, 19); Team NewZealand = new Team("New Zealand", .8, 20); Team Nigeria = new Team("Nigeria", 3.3, 21); Team Paraguay = new Team("Paraguay", 3.6, 22); Team Portugal = new Team("Portugal", 4.4, 23); Team Serbia = new Team("Serbia", 3.7, 24); Team Slovakia = new Team("Slovakia", 2.6, 25); Team Slovenia = new Team("Slovenia", 2.7, 26); Team SouthAfrica = new Team("South Africa", 2.9, 27); Team Spain = new Team("Spain", 4.9, 28); Team Switzerland = new Team("Switzerland", 2., 29); Team Uruguay = new Team("Uruguay", 3.8, 30); Team USA = new Team("USA", 3.9, 31); Teams.add(Algeria); Teams.add(Argentina); Teams.add(Australia); Teams.add(Brazil); Teams.add(Cameroon); Teams.add(Chile); Teams.add(IvoryCoast); Teams.add(Denmark); Teams.add(England); Teams.add(France); Teams.add(Germany); Teams.add(Ghana); Teams.add(Greece); Teams.add(Honduras); Teams.add(Italy); Teams.add(Japan); Teams.add(KoreaDPR); Teams.add(KoreaRepublic); Teams.add(Mexico); Teams.add(Netherlands); Teams.add(NewZealand); Teams.add(Nigeria); Teams.add(Paraguay); Teams.add(Portugal); Teams.add(Serbia); Teams.add(Slovakia); Teams.add(Slovenia); Teams.add(SouthAfrica); Teams.add(Spain); Teams.add(Switzerland); Teams.add(Uruguay); Teams.add(USA); } } ------------------------------------- import java.util.ArrayList; public class Team { protected String TeamName; protected double TeamOdds; protected int TeamID; protected int Wins; protected int Losses; protected int Ties; protected double AvgGoals; //Average goals scored per match protected int GoalsScored; protected int GoalsLetIn; public Team (String Name, double d, int id) { TeamName = Name; TeamOdds = d; TeamID = id; Wins = 0; Losses = 0; Ties = 0; AvgGoals = 0; GoalsScored = 0; GoalsLetIn = 0; } void play (int Team1) { Teams.get(Team1); } } ------------------------------------------------
And btw, I realize thatt I have to make the teams do something (not just put a semicolon), but It can't even find the arraylist.Last edited by Kingarmy; 12-28-2010 at 07:55 PM. Reason: Added code tags
- 12-28-2010, 08:00 PM #2
the Teams member is declared inside the main() method. which means that any other method would not have visibility to see it. So in this case, declaring the property as a static member of the class should work.
In this case, because you create an instance of a Team object, but do not use the reference to it other than to add it to the array, we can simplify this by doing the create and add to an array in one step.
here I have moved this to its own init() method as well, in case we later on don't want to initialize this every time the main() is ran, such as when you have persistent storage for these.
I implemented a static getTeam() method that uses an also new getName() method added to the team.
Because the teams are stored into a list and not a map keyed by team name, we need to scan the array for each item comparing the team name until one matches.Java Code:import java.util.ArrayList; public class FIFA { static ArrayList<Team> teams = new ArrayList<Team>(); public static void init() { teams.add(new Team("Algeria", 1.7, 0)); teams.add(new Team("Argentina", 4.6, 1)); teams.add(new Team("Australia", 3.7, 2)); teams.add(new Team("Brazil", 4.7, 3)); teams.add(new Team("Cameroon", 3.8, 4)); teams.add(new Team("Chile", 3.9, 5)); teams.add(new Team("Côte d'Ivoire", 4.3, 6)); teams.add(new Team("Denmark", 3.6, 7)); teams.add(new Team("England", 4.8, 8)); teams.add(new Team("France", 4.6, 9)); teams.add(new Team("Germany", 4.7, 10)); teams.add(new Team("Ghana", 3.3, 11)); teams.add(new Team("Greece", 2.7, 12)); teams.add(new Team("Honduras", 1.3, 13)); teams.add(new Team("Italy", 4.6, 14)); teams.add(new Team("Japan", 4.6, 15)); teams.add(new Team("Korea DPR", .7, 16)); teams.add(new Team("Korea Republic", .9, 17)); teams.add(new Team("Mexico", 3.1, 18)); teams.add(new Team("Netherlands", 4.6, 19)); teams.add(new Team("New Zealand", .8, 20)); teams.add(new Team("Nigeria", 3.3, 21)); teams.add(new Team("Paraguay", 3.6, 22)); teams.add(new Team("Portugal", 4.4, 23)); teams.add(new Team("Serbia", 3.7, 24)); teams.add(new Team("Slovakia", 2.6, 25)); teams.add(new Team("Slovenia", 2.7, 26)); teams.add(new Team("South Africa", 2.9, 27)); teams.add(new Team("Spain", 4.9, 28)); teams.add(new Team("Switzerland", 2., 29)); teams.add(new Team("Uruguay", 3.8, 30)); teams.add(new Team("USA", 3.9, 31)); } public static Team getTeam(String name) { for (Team team : teams) { if (team.getName().equals(name)) { return team; } } return null; } public static void main (String[] args) { init(); } } ------------------------------------- import java.util.ArrayList; public class Team { protected String TeamName; protected double TeamOdds; protected int TeamID; protected int Wins; protected int Losses; protected int Ties; protected double AvgGoals; //Average goals scored per match protected int GoalsScored; protected int GoalsLetIn; public Team (String Name, double d, int id) { TeamName = Name; TeamOdds = d; TeamID = id; Wins = 0; Losses = 0; Ties = 0; AvgGoals = 0; GoalsScored = 0; GoalsLetIn = 0; } void play (int Team1, int Team2) { Teams.get(Team1); Teams.get(Team2); } public String getName() { return TeamName; } } ------------------------------------------------
Similar Threads
-
Class Instances stored in an ArrayList
By Ersk in forum New To JavaReplies: 4Last Post: 12-12-2009, 04:13 PM -
[newbie] getting class members from Arraylist
By jon80 in forum New To JavaReplies: 16Last Post: 05-15-2009, 07:45 AM -
Using arrayList to store data from one class to another.
By nickc88 in forum New To JavaReplies: 3Last Post: 03-28-2009, 05:02 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Help passing arraylist to another class
By adlb1300 in forum New To JavaReplies: 3Last Post: 11-06-2007, 08:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks