简体   繁体   中英

How to add different types of values into arraylist<object>?

So basically I have a

public class Tournament {

private int [] par; // stores par for each hole in the golf course.
private ArrayList<Player> players; // stores the list of players.

public static final int NUMHOLES = 18;  // constant.

And

public ArrayList<Player> getPlayers() {
    return players;
}

But now I want to enter a name and handicap to the Players arrayList.

public void enter(String aName, int aHandicap) {

}

How do I create the enter() method?

I have tried

//players.add(aName); players.add(aHandicap);
//players.addAll(aName, aHandicap)); 

and no dice. ANY HELP APPRECIATED

You are getting your abstractions wrong . His name and handicap are a property of that player person. It is not a property of the list!

So, you add fields to your Player class, and then you initialize them either directly in the constructor, or you provide setter methods. Conceptually, the name should probably come in via the constructor - because the name should be immutable, and not change after creation of a Player. Whereas the handicap is likely to change; so there a setter method would be the better choice!

And then, you can iterate your list of Player objects, and call those setter methods on the Player objects, not the list.

In other words: your classes / objects are a model of reality. So your player class should represent those aspects of a "human player being" that make sense given the greater context of your model.

And even when you think that your Player class should not know about such things, you still create an abstraction, like:

public class EnhancedPlayer { // could extend Player
  that has those other fields

You see, your question would turn into: lets represent several pieces of information that belong together ... by putting them in different lists or so; and a "common" index would mean: stuff belongs together.

Finally, the direct answer: you can't do that; and you shouldn't do that. When you have a List<Player> , then you can only add player objects to that list!

The name and the handicap of a Player are part of its identity such that they should be both fields of the class Player so:

Assuming that your class Player looks like:

public class Player {
    private String name;
    private int handicap;

    public Player(final String name, final int handicap) {
        this.name = name;
        this.handicap = handicap;
    }

    // Getters (and setters if needed)
}

Then your method enter would be:

public void enter(String aName, int aHandicap) {
    players.add(new Player(aName, aName));
}

You're trying to add Strings and numbers to a list of Players.

You need to create a Player first, then add the Player to the list. It won't auto-create a player for you just because you hand it the pieces needed to construct one.

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