【问题标题】:Java ArrayList in ArrayListArrayList 中的 Java ArrayList
【发布时间】:2013-11-20 16:33:57
【问题描述】:

我有一个关于 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(); 好像不行。

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

【问题讨论】:

  • allSpawnpoints.get("yourWorld") 将返回一个List&lt;Location&gt; ..所以它就像List&lt;Location&gt; loc = allSpawnpoints.get("yourWorld"); loc.size()

标签: java rr


【解决方案1】:

尝试使用MapWorldListLocation,而不是ArrayListWorld 对象。像这样的:

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

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

allSpawnpoints.put(yourWorld,Spawnpoints);

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

我还建议将Spawnpoints 重命名为spawnPoints

【讨论】:

  • 谢谢,我会这样做的!
  • 我在我的问题中添加了一些内容,您可以查看一下吗?
  • 您必须遍历 Map 对象,然后您将能够获取每个键的值,您可以将其值存储到列表中,然后计算大小。
  • 这样吗? for (Entry> entry : allSpawnpoints.entrySet()) { if (entry.getKey() == "YourWorld") { int number = entry.getValue().size();}}
  • 查看this 了解如何在 Java 中迭代地图
猜你喜欢
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
相关资源
最近更新 更多