2014-01-08 106 views
-1

有人能告诉我下面的代码有什么问题吗?当我试运行后的第一行代码如下{产生以下错误:使用ArrayList的哈希映射

“java.lang.IndexOutOfBoundsException:指数:0,大小:0”

public static class ASIFFile { 
      private ArrayList<HashMap<String,String>> data; 
      private static int currRec = 0; //assign each record a numeric id based on this figure. 

      // Method for reading ADIFfile 
      public ArrayList<HashMap<String,String>> ReadASIFfile (File DataFile) {  
       data = new ArrayList<HashMap<String, String>>(500); 

       try { 
         HashMap<String, String> temp = new HashMap<String,String>(10); 
         data.set(currRec, temp);       
         (data.get(currRec)).put("recID", Integer.toString(currRec));//give the record a numeric ID 


... 
+1

第二行吠叫了分号:) – Eich

+0

@Eich啊,这是一个错误的复制到文本框。它实际上在代码 – Adam

回答

2

正如凯文提到的,你从来没有在你的ArrayList中放入任何东西。你的代码应该是这个样子......

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

try { 
    //give the record a numeric ID 
    data.add(currRec, temp); 

在以前的解决方案,你调用在data.set(currRec,温度);它假定ArrayList的位置currRec中有一个HashMap元素。如果你还没有在ArrayList中放入任何东西,那么就不会有currRec元素。

+0

感谢您的帮助,问题是我在做data.set而不是data.add! – Adam

6

你永远不添加任何东西到您的ArrayList。你有一个ArrayList可以持有HashMaps,但目前empty.You有一个新的HashMap添加到您想要使用该索引之前使用索引。

+0

中,我将它改为'HashMap temp = new HashMap (10); \t \t \t \t \t在data.set(currRec,温度); \t \t \t(data.get(currRec))把( “recID”,Integer.toString(currRec));'但是我仍然有问题。你有什么其他的建议? – Adam

+0

@Adam,你可以以[SSCCE](http://sscce.org)的形式发布你的代码吗? –