当数组在对象变量中时访问单个数组索引

问题描述 投票:0回答:1

所以我有一个在主程序中创建的数据类鞭打,这个类里面是一个包含x个玩家及其变量的玩家的实例变量

public class Whiplash {

private int rounds;
private WhiplashQuestions questions;
private Player[] players;

public Whiplash(int ROUNDS, WhiplashQuestions QUESTIONS, Player[] PLAYERS) {
    setRounds(ROUNDS);
    setPlayers(PLAYERS);
    setWhiplashQuestions(QUESTIONS);
}



public int getRounds() {
    return rounds;

}

public void setRounds(int rounds) {
    this.rounds = rounds;
}

public Player[] getPlayers(int id) {
    return players[id];
}

public void setPlayers(Player[] players) {
    this.players = players;
}

 public WhiplashQuestions getQuestions() {
    return questions;
}

public void setWhiplashQuestions(WhiplashQuestions question) {
    this.questions = question;
}

}

public class Player {


private int id;
private int points;
private String player;

   public Player(int ID, int POINTS, String PLAYER) {
    setId(ID);
    setPoints(POINTS);
    setPlayer(PLAYER);
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getPoints() {
    return points;
}

public void setPoints(int points) {
    this.points = points;
}

public String getPlayer() {
    return player;
}

public void setPlayer(String player) {
    this.player = player;
}

}

else if (command.startsWith("#whiplash")) {
        int usercount = getUserCount(client);
        String users[] = getUsersInRoom(client);
        Player[] player = new Player[usercount];

        player = convertUsersToPlayers(users, usercount);
        WhiplashQuestions q = new WhiplashQuestions("Whats your favorite color? ");

        Whiplash wl = new Whiplash(10, q, player);
        startWhiplash(wl);
public Player[] convertUsersToPlayers(String[] users, int userCount) {
    Player p[] = new Player[userCount];

    int u = users.length;

    for (int i = 0; i < users.length; i++) {
        p[i] = new Player(i, 0, users[i]);
    }

    return p;
}

public int getUserCount(ConnectionToClient client) {
    String room = client.getInfo("room").toString();
    int count = 0;

    Thread[] clientThreadList = getClientConnections();

    for (int i = 0; i < clientThreadList.length; i++) {

         ConnectionToClient clientProxy = ((ConnectionToClient) clientThreadList[i]);
        if (clientProxy.getInfo("room").equals(room)) {
            count++;
        }
    }

    return count;
}

public String[] getUsersInRoom(ConnectionToClient client) {
    String room = client.getInfo("room").toString();
    int connectedUsers = getUserCount(client);
    String[] usersInRoom = new String[connectedUsers];

    Thread[] clientThreadList = getClientConnections();

    for (int i = 0; i < clientThreadList.length; i++) {

        ConnectionToClient clientProxy = ((ConnectionToClient) clientThreadList[i]);

        if (clientProxy.getInfo("room").equals(room)) {
            try {
                //System.out.println(clientProxy.getInfo("userName".toString()));
                usersInRoom[i] = clientProxy.getInfo("userName".toString()).toString();

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    return usersInRoom;
}

因此,在我的主程序中,我通过将玩家字符串转换为玩家类型,然后传递正确的值来创建一个挥鞭子的实例。我希望能够从主程序访问各个Player []索引,并对这样做的语法感到困惑。例如,在主程序中,如果我想获得第三个玩家玩家字符串的打印鞭打实例,我可以按照System.out.print(whiplash.getPlayers [3] .getPlayers()。toStrng( )); ?

java arrays oop
1个回答
0
投票

所以这是我在循环中寻找和工作的语法(与i循环)

 wl.getPlayers()[i].getPlayer();
© www.soinside.com 2019 - 2024. All rights reserved.