【发布时间】:2016-07-20 23:15:38
【问题描述】:
我有一种方法可以让所有玩家参与游戏。问题是我想将它们分成两个团队。但是我怎样才能返回一张桌子?我已经尝试过 2D 阵列,但我不知道如何将玩家放在最后一个位置。我还看到了使用 2D ArrayLists 的选项,但这看起来非常复杂。有没有优雅的方法来解决这个问题?
编辑(我当前的代码):
public String[][] getGameMembers()
{
if(!isIngame())
{
return null;
}
//Return-Array
String[][] playerTable = new String[2][6];
//Get all Players
List<Participant> l = game.getParticipants();
//Put each player in the Arraylist
for(int i = 0; i < l.size(); i++)
{
Participant s = l.get(i);
//Get teams and put in the right place in array
if( l.get(i).getTeam() == Side.BLUE )
{
playerTable[0][playerTable.length] = s.getSummonerName() + " (" + s.getChampion() + ")" ;
}
else
{
playerTable[1][playerTable.length] = s.getSummonerName() + " (" + s.getChampion() + ")" ;
}
}
return playerTable;
}
此代码不起作用,因为我不知道如何将元素放在数组中的最后位置。
【问题讨论】:
-
你的代码在哪里?
-
您可以返回一对,一个包含两个元素的数组(都是球员列表),一个地图,其中键是球队名称,值是球员列表,甚至是您自己的数据结构。贴一些代码
-
ArrayList 是要走的路……这并不难。阅读
Collections学习路径和谷歌一些java arraylist examples -
你从你所谓的“让所有玩家参与游戏”的方法中究竟得到了什么(数据结构)?
标签: java arrays arraylist multidimensional-array return