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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当数组在对象变量中时访问单个数组索引相关的知识,希望对你有一定的参考价值。

所以我有一个在主程序中创建的数据类鞭打,这个类里面是一个包含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( )); ?

答案

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

 wl.getPlayers()[i].getPlayer();

以上是关于当数组在对象变量中时访问单个数组索引的主要内容,如果未能解决你的问题,请参考以下文章

当响应包含在数组中时,RestKit POSTed 托管对象变得重复

我似乎无法访问存储在数组位置 0 和 1 中的值

当对象位于对象数组中时如何从状态中删除对象

Hashmap 与数组性能

无法从函数返回的变量索引到空数组,或访问返回的正则表达式数据时出现问题

面向对象基础4.5-4.7