Method to save multiple "Players" to a list

Asked

Viewed 99 times

3

I want to make a system of party, the only command will be /party PLAYER (if the player is already in the party, it will be removed and if it is not, it will be added).

But I couldn’t find a way to do it, I tried with HashMap using <Player, String[]> but it would not be possible to add a new player in party as a number determined in String[] string = {"Player1", "Player2", "Player3"}.

I tried with ArrayList, but I couldn’t either, so I was wondering how I could do that.

  • 1

    Why didn’t you get ArrayList?

  • Why every player you type /party PLAYER needs a ArrayList new.

  • Edit the question with the code you already have, I think I misunderstood the question.

  • But like this, you want to wear one "lista" to store/add/remove players, right?

  • I want a String[] party that you can use party.add(PLAYER);

3 answers

5


You can do it with ArrayList, as follows:

ArrayList<String> players = new ArrayList<String>();
//para adicionar
players.add("novo_registro");
//para remover
players.remove(novo_registro");

EDIT - to create as you said in the comment:

HashMap<String, ArrayList<String>> parties = new HashMap<String, ArrayList<String>>();
  • But for example the party of player1 contains (player2,player3) and the party of player5 contains (player6,player7,player8), how would I do that ??

2

Use a hashmap that saves the main player (who created the party) and as value save an arraylist of type Player[]

2

Try it like this:

import java.util.LinkedHashSet;
import java.util.Set;

public class Party {

    private final Set<String> players;

    public Party() {
        this.players = new LinkedHashSet<>();
    }

    public void toggle(String player) {
        if (players.contains(player)) {
            players.remove(player);
        } else {
            players.add(player);
        }
    }

    public String[] toArray() {
        return players.toArray(new String[players.size()]);
    }

    public Set<String> toSet() {
        return new LinkedHashSet<>(players);
    }
}

Here is an example/test of how to use the class Party:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Party p = new Party();
        p.toggle("Pedro"); // Adiciona o Pedro
        System.out.println(Arrays.asList(p.toArray())); // Imprime [Pedro]
        p.toggle("Maria"); // Adiciona a Maria
        System.out.println(Arrays.asList(p.toArray())); // Imprime [Pedro, Maria]
        p.toggle("Pedro"); // Remove o Pedro
        System.out.println(Arrays.asList(p.toArray())); // Imprime [Maria]
        p.toggle("Carlos"); // Adiciona o Carlos
        p.toggle("Fernando"); // Adiciona o Fernando
        System.out.println(p.toSet()); // Imprime [Maria, Carlos, Fernando]
        p.toggle("Carlos"); // Remove o Carlos
        System.out.println(p.toSet()); // Imprime [Maria, Fernando]
    }
}

Watch it run on ideone

Note that you add/remove players with the method toggle(String). To get the list of players as a array. use the method toArray(). If you prefer to work with the list of players in the form of a Set, use the method toSet(). In the test, I use both in order to show how they can be used.

Modify the array returned by toArray() will not interfere with Party, and change the Party will not interfere with arrays previously obtained through the toArray(). Likewise, modify the Set returned by toSet() does not interfere with Party and change the Party does not interfere with us Sets previously obtained by toSet(). This ensures that changes in Party do not cause unforeseen side effects on arrays and Setpreviously obtained, and also ensures that changes in these arrays and Setdo not cause unforeseen side effects on Party.

In addition, it should be noted that the players will always be kept in the order in which they were entered. This is because we are using a LinkedHashSet. If instead a HashSet, the order of players would be random. If a TreeSet, the players would be automatically ordered alphabetically.

I’m also assuming that the Party will be used in a single thread. If this is not the case, the simplest way to make it safe to use in multiple threads is to put the modifier synchronized in the methods toggle(String), toArray() and toSet().

Browser other questions tagged

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