简体   繁体   中英

Creating an array of arrays in Java

Say I have a Player class such as:

public class Player {

String name;
int chips;
int betVal;

}

Is the following code correct for creating the array of the players?

public static void main(String[] args) {
    int playerCount;
    int startingChip;
    out.print("How many players? ");
    playerCount = myScanner.nextInt();
    Player[] aPlayer = new Player[playerCount + 1];

    for (int i = 0; i < playerCount + 1; i++){
        aPlayer[i] = new Player();
    }

    out.print("Enter starting chip amount: ");
    startingChip = myScanner.nextInt();

}

If so, how would I assign the name, chip amount and the betVal to each player? How would I access and alter them later on in the code?

EDIT: Will it be easier leaving the Player as an object or an array (name,chips,betVal) for accessing it later on?

First off, your variables need to be private (there's a section of programmers who prefer public variables but most prefer private).

You could set the values of the Player object either through an overloaded constructor or through the setters.

public class Player {


    private String name;
    private int chips;
    private int betVal;


    public Player(){
    //default constructor to initialize without any parameters
    }

    public Player(String name, int chips, int betVal){
        this.name=name;
        this.chips=chips;
        this.betVal=betVal;

    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChips() {
        return chips;
    }

    public void setChips(int chips) {
        this.chips = chips;
    }

    public int getBetVal() {
        return betVal;
    }

    public void setBetVal(int betVal) {
        this.betVal = betVal;
    }
}

In which case, your initialization could be either

aPlayer[i] = new Player("Jason", 5,1000);

or using the setters as in

for (int i = 0; i < playerCount + 1; i++){
        aPlayer[i] = new Player();
 }


 aPlayer[i].setName("Jason");
 aPlayer[i].setChips(5);
 aPlayer[i].setBetVal(1000);

Considering your sample program, I guess option 2 plays well.

You can access each player by telling the array which one you want to access.

`aPlayer[0].name = "JAG";`

would work for example.

Why not ask all the user's inputs before creating anything ? For exemple :

public static void main(String[] args) {
    int playerCount;
    int startingChip;

    out.print("How many players? ");
    playerCount = myScanner.nextInt();
    out.print("Enter starting chip amount: ");
    startingChip = myScanner.nextInt();

    Player[] aPlayer = new Player[playerCount];

    for (int i = 0; i < playerCount; i++){
        aPlayer[i] = new Player();
        aPlayer[i].setChips(startingChip);
    }
}

You should also use a List. And finally, if you want to ask for the name of each player, do it directly in the loop :

for (int i = 0; i < playerCount; i++){
    aPlayer[i] = new Player();
    aPlayer[i].setChips(startingChip);

    out.print("What's the player " + i + " name? ");
    aPlayer[i].setName(myScanner.next());
}

try something like this... ask for individual players info in the loop itself.

    System.out.println("How many players? ");
    Scanner myScanner = new Scanner(System.in);;
    playerCount = myScanner.nextInt();
    Player[] aPlayer = new Player[playerCount];

    for (int i = 0; i < playerCount; i++){
        aPlayer[i] = new Player();

        System.out.println("Enter Name for Player " + i+1);
        String name = myScanner.next();

        System.out.println("Enter chips for Player " + i+1);
        int chips = myScanner.nextInt();

        System.out.println("Enter betVal for Player " + i+1);
        int betVal = myScanner.nextInt();

        aPlayer[i].name = name;
        aPlayer[i].chips = chips;
        aPlayer[i].betVal = betVal;
    }

Also make the instance variables of Player class private, and access them using getters and setters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM