Arraylist in JAVA

Asked

Viewed 527 times

2

I have a question about Arraylist

Example: I made a graphical interface in Java to register players in a ArrayList. after registration, the player goes to a Jlist, Here comes my doubt. I want to buy this player for my team, I select it and click on buy and in case this method buy I only used a remove. I wanted to remove him from the list of players for sale and put him in mine Team?

  • 2

    could show your code up to the moment?

  • 2

    Paste the code in the post so you can understand better

  • 2

    If you put the code you have made so far we can put following your logic. Please provide the code.

  • I would like to add an addendum on Arraylist: here

2 answers

1

You have some alternatives, among them:

  • Add the player you selected to Arraylist Time before having it removed from the Arraylist of 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.*/);
        }
    

    ...

1

Logically, the same way you have a ArrayList of "free" players, each object Time have to have your ArrayList of players.

Take a closer look at this... think of a player history, place an object Contrato in the middle, where you have the player id and the team id, as well as the start and end dates of the contract.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.