Method to save several "Players" in a list

3

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

But I did not find a way to do this, I tried with HashMap using <Player, String[]> but I would not have to go adding a new player in party because it is a number determined in String[] string = {"Player1", "Player2", "Player3"} .

I tried with ArrayList , but I also could not, so I would like to know a way to get it done.

    
asked by anonymous 25.07.2015 / 01:41

3 answers

5

You can do this 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>>();
    
25.07.2015 / 01:53
2

Try 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's an example / test of how to use the Party class:

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]
    }
}

See rolling in ideone

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

Modifying the array returned by toArray() will not interfere with Party , and changing Party will not interfere with arrays previously obtained through toArray() . Likewise, modifying Set returned by toSet() does not interfere with Party and changing Party does not interfere with Set s previously obtained by toSet() . This ensures that changes in Party do not cause unanticipated side effects in arrays and Set s previously obtained, and also ensures that changes in these arrays and Set s do not cause unanticipated side effects in Party .

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

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

    
25.07.2015 / 02:32
2

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

    
25.07.2015 / 01:58