English 中文(简体)
如何随机化并将ArrayList<String>拆分为两个偶数ArrayList
原标题:How to randomize and then split an ArrayList<String> into two even ArrayLists

我几乎不懂java,但我正在努力为这个项目学习一些。我正在尝试修改一个名为GyaPickupBot的程序,它基本上是IRC上的一个“拾取游戏”机器人,玩家可以在其中键入:!添加,以便添加到想要玩游戏的玩家列表中,并添加足够多的玩家!添加机器人程序宣布游戏将在游戏服务器(即地震服务器)上进行。现在,当指定数量的玩家!添加并启动游戏,机器人只列出以前的玩家!在达到最大玩家数量之前添加ed。我希望它能瓜分所有的球员!随机分成两个大小相等的小组。我已经用Collections.shuffle完成了随机部分,但我不知道如何将球员分成两个同等规模的团队。几周前,我给驻日本的作者发了一封电子邮件,他终于在今天早上回复了我,并就如何做到这一点给出了一些非常模糊的提示:

Well... at this time, I don t have much motivation to maintain this code. I can only suggest you some hints.

private boolean handleReady(String channel, String sender, String login, String hostname, String message) {
boolean isUpdate = false;
String readyGameID = mgr.getReadyGameID();
if (null != readyGameID) {
// *** add some code here to choose teams and store that result to string variable. something like: "team1: ,,,, team2: ,,,,,"
// *** you can get players list by mgr.getPlayers(readyGameID) in order to divide players to 2 teams randomly 

for (String ch : getChannels()) {
sendMessage(ch, mgr.getPickupReadyString(readyGameID));
// sendNotice(ch, mgr.getPickupReadyString(readyGameID));
// *** then, send that string to channel
}
mgr.setLastGame(Calendar.getInstance().getTimeInMillis(), mgr.getPickupReadyString(readyGameID));
ArrayList<String> players = mgr.getPlayers(readyGameID);
for (String nick : players) {
sendNotice(nick, mgr.getPickupReadyPMString(readyGameID));
// *** and send that string to players too
}
isUpdate = mgr.clearPlayers(players);
}
return isUpdate;

我知道这可能比这里要求的要多,但我真的在努力学习,但我想不出来,如果有任何帮助,我将不胜感激

最佳回答

下面的代码未经测试,但应该会让您了解如何继续。祝你好运

// retrieve all players
ArrayList<String> players = mgr.getPlayers(readyGameID);
// randomize the list
Collections.shuffle(players);
// instantiate two arraylists for the teams
ArrayList<String> teamRed = new ArrayList<String>();
ArrayList<String> teamBlue = new ArrayList<String>();

// add the first half of players to teamRed
teamRed.addAll(players.subList(0, players.size() / 2 + players.size()%2));
// and the second half to teamBlue
teamBlue.addAll(players.subList(players.size() / 2 + players.size()%2, players.size()));

// now do whatever you need to do with the two teams
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签