ArrayList in JAVA

2

I have a question about ArrayList

Example: I made a graphical interface in Java to register players in ArrayList . after registration, the player goes to a Jlist , then my question comes. I want to buy this player for my team, I select it and click on buy and in case this method buy I just used a remove. Would you like to remove it from the list of players that are for sale and put it on my Time ?

    
asked by anonymous 04.06.2014 / 14:02

2 answers

1

By logic, just as you have ArrayList "free" players, each Time object must have its ArrayList of players.

Consider this better ... think of a player's history, put a Contrato object in the middle, where you have the player id and team id, in addition to the start and end dates of the contract. >     

04.06.2014 / 16:11
1

You have some alternatives, among them:

  • Add the player you selected in ArrayList Time before you have removed it from ArrayList players for sale:

    public class Time {
        private ArrayList<Jogador> time; //ArrayList do seu time.
        public Time() {
            time = new ArrayList<>();
        } public adicionarJogador(Jogador a) {
            time.add(a);
        }
    }
    //
    public class Vendedor {
        private ArrayList<Jogador> vendavel; //ArrayList de jogadores à venda, supondo que já existem jogadores nesse ArrayList.
        Time a = new Time();
        public venderJogador() {
            a.adicionarJogador(/*Índice do jogador que você selecionar através da interface.*/);
            vendavel.remove(/*Índice do jogador que você selecionar através da interface.*/);
        }
    

    ...

05.06.2014 / 03:26