40994

Edit Specific Instance of Object

Question:

Say i let players create teams and creating a team calls a new instance of the team class which has an array list called members.

Now in the main class how would i add a player to a team after being invited? i have an addPlayer method in the team class that simply add them to the arraylist but what if there are currently multiple instances of the teams class(other players have created teams) how would it know which one to join?

I do have a variable in the Teams class for the teamLeader which gets set when creating the instance if that could help me edit a certain instance.

Team team = new Team(this, leader);

Any help is appreiciated

Answer1:

You need an identifier to uniquely distinguish each team and you can use that identifier to store the teams in a Map. Something like this:

Map<String,Team> teamMap = new HashMap<String,Team>();

Chose the key type as per your requirement, I chose String for an example

Answer2:

As per your design, You need to keep all teams in a list after creation.

ArrayList teamsList=new ArrayList (); Team team = new Team(this, leader); teamsList.add(team);

Then Loop through all teams in addPlayer method and then compare leader and then add a player to it. Something like this -

public void addPlayer (Player player,String leader){ for(int i=0; i<teamListSize;i++) Team tempTeam=teamsList.get(i); if(tempTeam.getLeader().equalsIgnoreCase(leader)){ tempTeam.add(player); break; } }

Recommend

  • Restkit + Objective-c - Multiple calls to same web service
  • implementing HttpSessionListener
  • mac os jdk 1.8 problems vlc control JAWT not load
  • Subclipse- update working copy
  • Get VS to highlight all .Net 3.5 SP1 method calls
  • GORM Mapping two attributes of same Class with hasMany
  • How will Roslyn help me in avoiding a recompile to deploy changes to my ASP.NET website?
  • ELMAH: Can you set it up to email errors only remotely?
  • Find symbol dependencies in source code [closed]
  • How to apply async task into this
  • Many to many relationship core data query for specific collection
  • LNK1104: cannot open file 'kernel32.lib'
  • Invert string in Rust
  • Less Conflicting Session Manager for Zope 2
  • drawing random circles, storing their coorindates in an array
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • Who propagate bugfixes across branches (corporate development)?
  • calculate gradient output for Theta update rule
  • Not able to aggregate on nested fields in elasticsearch
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Android screen density dpi vs ppi
  • Bug in WPF DataGrid
  • Incrementing object id automatically JS constructor (static method and variable)
  • Javascript Callbacks with Object constructor
  • How to check if every primary key value is being referenced as foreign key in another table
  • How to handle AllServersUnavailable Exception
  • Trying to switch camera back to front but getting exception
  • How can I use Kendo UI with Razor?
  • Symfony2: How to get request parameter
  • jQuery tmpl and DataLink beta
  • How to get next/previous record number?
  • GridView Sorting works once only
  • WPF Applying a trigger on binding failure
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • unknown Exception android
  • Programmatically clearing map cache
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?