1

我对 Arraylist 中的 ArrayList 有疑问。这是关于具有多个产卵的多个世界。我想一一检查每个世界,并将该世界的所有生成物保存在 ArrayList 中。最后,我有一个 ArrayList,在每个位置(每个位置都是一个世界)上都有另一个 ArrayList,其中包含该世界的生成物。

如果我不知道会有多少个产卵或世界,我该怎么做?我想到了这个:

只看一个世界:

public ArrayList<Location> Spawnpoints = new ArrayList<Location>(); 
//ArrayList containing all the spawns of this world).

展望世界上的每一个产卵

 Spawnpoints.add(new Location(spawnX , spawnY , spawnZ)); 
//Adding every spawn to the ArrayList spawnpoints.

因此,在我查看了一个世界之后,我已经用位置填充了 ArrayList 生成点。现在我想将 ArrayList 生成点添加到一个新的 ArrayList 世界。之后,我将为下一个世界重复上面的代码,直到我拥有所有的世界。

编辑。我认为它正在工作。当我只有名字时,我很难获得列表的大小。

所以假设我这样做了:allSpawnpoints.put("yourWorld",Spawnpoints); 现在我想获取字符串 yourWorld 的 Spawnpoints 列表的大小。知道我该怎么做吗?

我试过了:int number = allSpawnpoints.get("yourWorld").size(); 看起来这不起作用。

我希望有一个人可以帮助我!谢谢阅读。问候。

4

1 回答 1

3

尝试使用 a of to of代替一个ArrayListor对象。像这样的东西:WorldMapWorldListLocation

Map<World,List<Location>> allSpawnpoints = new HashMap<World,List<Location>>();

然后,在您创建了生成点列表后,World将其放入您的列表中,Map如下所示:

allSpawnpoints.put(yourWorld,Spawnpoints);

...然后进入下一个世界。

我还建议重命名SpawnpointsspawnPoints.

于 2013-11-20T16:44:43.710 回答