简体   繁体   English

我应该使用哪种类型的数组

[英]Which type of array should I use

I want to create an array with a list of players and which arena they are in. I don't know which type of array would be best suited. 我想创建一个包含玩家列表以及他们所在的竞技场的数组。我不知道哪种类型的数组最适合。

It will hold information like this: 它将包含以下信息:

PlayerName : Arena
John : Arena1
Harry : Arena1
Dave : Arena2
Will : Arena2

You're help is much appreciated, I am new to Java and can't see this listed on the site, if it is please point me in that direction :) 非常感谢您的帮助,我是Java的新手,并且无法在网站上看到此列表,如果是的话,请指向我:)

That's a mapping from players to arenas. 那是从球员到竞技场的映射。

I would create a class Player and a class Arena , so your data structure will be: 我将创建一个class Player和一个class Arena ,因此您的数据结构将是:

Map<Player, Arena> playerToArena = new HashMap<Player, Arena>();

I wouldn't use an array in this case, but, if you insist, you can write something similar to this: 在这种情况下,我不会使用数组,但是,如果您坚持要写,可以编写类似于以下内容的内容:

class Pair { 
    Player player;
    Arena arena;
}

Pair[] array = new Pair[10];
// or
List<Pair> arrayList = new ArrayList<Pair>();

This could be achieved using a Map. 这可以使用地图来实现。 Eg, 例如,

Map<String, String> map = new HashMap<String, String>();
map.put("name", "demo");
map.put("fname", "fdemo");
// etc

map.get("name"); map.get( “名称”); // returns "demo" Even more accurate to your example would be to declare: //返回“演示”,对您的示例更准确的是声明:

List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
data.add(0, map);
data.get(0).get("name"); 

There is only one type of array. 只有一种类型的数组。 In an array, you put in an index, and you get out the corresponding item. 在数组中,放入索引,然后取出相应的项。 If you want to use an array to represent this information, I would make a class that contains where you can set the Player and the Arena values, and then store each object in the array. 如果要使用数组表示此信息,我将创建一个包含可以在其中设置Player和Arena值,然后将每个对象存储在数组中的类。

You could create a container class that contains a playername and arena name and create an array (or other collection of that) 您可以创建一个包含玩家名称和竞技场名称的容器类,并创建一个数组(或该数组的其他集合)

private static final class ArenaPlayerContainer {
    private final String playerName;
    private final String arenaName;

    public String getPlayerName() {
        return playerName;
    }

    public String getArenaName() {
        return arenaName;
    }

    private ArenaPlayerContainer(String playerName, String arenaName) {

        this.playerName = playerName;
        this.arenaName = arenaName;
    }
}

then you can just create a ArenaPlayerContainer[] arr = new ArenaPlayerContainer[10]; 那么您只需创建一个ArenaPlayerContainer[] arr = new ArenaPlayerContainer[10]; for example 例如

I don't quite get it, but I am assuming you want to user a hashmap or hashtable instead (use hashtable if you are using threads). 我不太了解,但是我假设您想使用一个哈希表或哈希表(如果使用线程,请使用哈希表)。

HashMap HashMap中

This way you can extend PlayerName, Stack, Overflow, Word, Press with some custom class and use it with the map: HashMap <CustomClass, Arena> 这样,您可以使用一些自定义类扩展PlayerName,Stack,Overflow,Word,Press,并将其与地图一起使用: HashMap <CustomClass, Arena>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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