So I am trying to divide an array into 2 other arrays evenly and randomly in a Method.
I am very new to Java, and I don't know almost any Array methods, and don't know where to find them.
Here is what I have so far:
public void makeTeams(){
Player[] online = this.getServer().getOnlinePlayers();
Player[] team1;
Player[] team2;
}
I am using the Player type from Bukkit, a Minecraft API.
The Collections
framework has more goodies - you should in general avoid arrays unless you really need to use them. Nevertheless, since you have asked for arrays, here's how you can use Collections
to do the heavy lifting for you.
To create two teams of equal size, selecting team members randomly from a group:
Player[] online = getServer().getOnlinePlayers(); // don't need to code "this."
List<Player> list = Arrays.asList(online);
Collections.shuffle(list);
Player[] team1 = list.subList(0, list.size() / 2).toArray(online);
Player[] team2 = list.subList(list.size() / 2, list.size()).toArray(online);
This code cater for an odd number of players in the group.
If you were working with Collections (a Set would be the right Collection to use) and not arrays, the task would be far simpler.
This will get you a random item from the array:
Player playa = online[Math.floor(Math.random()*online.length)];
Just loop through the online array and fill up team1 and team2 one after another. Make sure not to use a player twice.