【问题标题】:Java cannot create List of HashMap sJava 无法创建 HashMap 的列表
【发布时间】:2013-11-01 17:56:46
【问题描述】:

我正在尝试将多个 hashmap 对象添加到列表中。第一个添加OK。但在下一轮它崩溃了异常错误“5”。我确实为每一轮创建了一个新的 hshmap 对象,但它仍然崩溃。

HashMap<String, Object> data;
ArrayList<HashMap<String, Object>> dataList = new ArrayList<HashMap<String, Object>>();

for(int i=0;i<iCount;i++)
    {
        arrRow = resSearchItems.get(i).split("\\^"); 

        {
            data = new HashMap<String, Object>();           

            data.put("ResNumber", arrRow[0]);
            data.put("MeetingType#", arrRow[1]);
            data.put("Topic", arrRow[2]);
            data.put("MeetingDate", arrRow[3]);
            data.put("Motion", arrRow[4]);
            data.put("Votes", arrRow[5]);

            dataList.add(data);

        }                       
    }

谢谢

【问题讨论】:

  • 请向我们展示实际的异常,而不是解释它
  • 另外,此代码无法编译,因此很难为您提供帮助。
  • 我现在完全猜到在你的第二次解析中没有arrRow[5]给他OutOfBoundsException at position 5。接受所有赌注。 :)
  • resSearchItems 在哪里声明和填充?
  • @trevor-e 我认为ExceptionError5 是一些未公开的秘密功能。

标签: java hashmap


【解决方案1】:

你应该检查arrRow.length -1 是你想要访问数组的最大索引。如果此值小于5,那么您将获得ArrayIndexOutOfBoundsException

【讨论】:

    【解决方案2】:

    resSearchItems.get(i) 其中i = 1,可能返回null,我们无法猜测。此外,如果 arrRow 不包含 6 个或更多元素,则将抛出 java.lang.ArrayIndexOutOfBoundsException

    【讨论】:

      【解决方案3】:

      两个想法:

      1. iCount 来自哪里?
      2. 尝试将检索数据的方法调用替换为:

        arrRow = "one^two^three^four^five^six".split("\\\\^");

      看看你的代码是否仍然崩溃。这对我来说编译并运行良好:

      import java.util.HashMap;
      import java.util.ArrayList;
      
      public class HashmapTest {
        public static void main(String args[]) {
          HashMap<String, Object> data;
          ArrayList<HashMap<String, Object>> dataList = new ArrayList<HashMap<String, Object>>();
          String[] arrRow;
          int iCount = 4;
      
          for(int i=0;i<iCount;i++) {
            arrRow = "one^two^three^four^five^six".split("\\^"); 
            data = new HashMap<String, Object>();           
      
            data.put("ResNumber", arrRow[0]);
            data.put("MeetingType#", arrRow[1]);
            data.put("Topic", arrRow[2]);
            data.put("MeetingDate", arrRow[3]);
            data.put("Motion", arrRow[4]);
            data.put("Votes", arrRow[5]);
      
            dataList.add(data);
      
          }                       
          System.out.println("hashmap count:" + dataList.size());
        }
      }
      

      这是某种抓取的数据吗?如果每个 resSearchItems 字符串不包含 6 个项目,您可能需要尝试以下操作:

       HashMap<String, Object> data;
       ArrayList<HashMap<String, Object>> dataList = new ArrayList<HashMap<String, Object>>();
       String[] hashKeys = {"ResNumber","MeetingType#","Topic","MeetingDate","Motion","Votes"};
      
       for(int i=0;i<iCount;i++) {
         arrRow = resSearchItems.get(i).split("\\^");  
         data = new HashMap<String, Object>();           
      
         for(int j=0;j<arrRow.length && j<hashKeys.length;j++) 
            data.put(hashKeys[j], arrRow[j]);
      
         dataList.add(data);
       }         
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        • 1970-01-01
        • 1970-01-01
        • 2018-02-17
        • 2017-11-11
        相关资源
        最近更新 更多