【问题标题】:create more than one map(collection) using for loop使用 for 循环创建多个地图(集合)
【发布时间】:2012-07-16 13:13:41
【问题描述】:

如何在 java.. 中使用 for 循环(或动态)创建多个地图(集合)?

List<Integer> temp = new ArrayList<Integer>();

for(int i=1; i<=10; i++)
{

   Map<Integer, Map> temp.get(i) = new HashMap<Integer, Map>();
}

考虑 temp 的值为“一”、“二”...“十”

请帮忙..

【问题讨论】:

  • 这是什么语言?也许是 Java?
  • temp 不能有值 "one","two"... "ten",因为它是 List&lt;Integer&gt;。你的意思是1, 2, ..., 10
  • -1 用于在不学习语言的情况下提出问题。
  • 什么类型的键和值的映射?你能详细说明一下吗?

标签: java collections


【解决方案1】:

这将为您创建一个包含 10 个地图的数组:

List<Map> temp = new ArrayList<Map>();

for(int i=1; i<=10; i++)
{

   temp.add(new HashMap<Integer, Map>());
}

【讨论】:

    【解决方案2】:

    不确定我是否完全理解你。这是你要找的吗?

    List<Map<Integer, Map> temp = new ArrayList<Map<Integer, Map>>();
    
    for(int i=1; i<=10; i++)
    {
       Map<Integer, Map> map = new HashMap<Integer, Map>();
       temp.add(map);
    }
    
    //Get the maps as you please here, from the tmp list
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-20
      • 2021-12-01
      • 2022-01-01
      • 1970-01-01
      • 2015-09-16
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多