1

Basically I have a team class, which has an array list that store the players in. In the driver class there's an arraylist that stores the teams.

Anyhow I've worked out how to add a player to a specific team and likewise remove a player from said team. Where I'm hitting problems is when I try to transfer one player to another.

My understanding is to scan through the first team, and get the player. Then somehow add this player to another, by scanning through the chosen team and add to it?

I've tried this way but it seems to replace the original player with the new player in both teams.

My other approach would be to somehow return the parameters of the player object, create another with the return parameters, remove the original then add the new instance in the other team?

Really not quite generally how I can go about this, been trying all afternoon!
If someone could offer me a general idea, then I can go off and apply the understanding to practice.

gnat
20.5k29 gold badges117 silver badges308 bronze badges
asked Nov 23, 2011 at 17:41
3
  • 3
    Please paste some code, that shows expected vs. actual Commented Nov 23, 2011 at 17:44
  • Are you removing from the first list, or just finding it? You need to find it, and actually remove it, then add it to the other list. Commented Nov 23, 2011 at 17:44
  • Removing, then adding Commented Nov 23, 2011 at 19:41

1 Answer 1

1

ArrayList has methods to find the location of an object, remove an object and add an object. As far as I can see, your question can be solved like this:

Given an object (or team player in question)
 1. Find the location (indexOf) of that object in first ArrayList
 2. Remove it from there (remove) and store the returned object
 3. Add it in the second ArrayList (add)

where the terms in the brackets are the methods from ArrayList APIs which you can find here and figure out the arguments which they take.

Hope that's what you meant.

answered Nov 24, 2011 at 16:33
1
  • 1
    I would also add some asserts before, after, and maybe in the middle. Commented Dec 25, 2011 at 21:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.