2012-04-21 40 views
1

下面的类是为了索引逐行的位置。 引发错误的方法意味着在当前索引上附加单独文档的索引。也就是说,如果有6条线路的头文件,附加文件的第一行应该被索引为7行NoSuchElementException中的每个循环与ArrayList <Integer>

public class DocumentIndex { 
    // a NavigableMap implementation to store indexed words and their locations 
    private TreeMap<String, ArrayList<Integer>> map = new TreeMap<String, ArrayList<Integer>>(); 

    /** 
    * A method to append another index onto the main index 
    * @param  indexAppendix  the additional index to be appended onto the main index 
    */ 
    public void append(DocumentIndex indexAppendix){ 
    if(indexAppendix == null){ 
     throw new NullPointerException(); 
    } 
    Integer docEnd = 0;         // the last line recorded in the main index 
    Set<String> set = map.keySet();      // a Set of the key values from map 
    //select each key 
    for(Iterator<String> iter = set.iterator(); iter.hasNext();){ 
     String key = iter.next();      // the current key value 
     // for each key select contents and determine the highest value 
     for(Iterator<Integer> iter2 = this.find(key).iterator(); iter2.hasNext();){ 
      Integer compare = iter2.next();    // the key index current value 
      if(compare>docEnd){ 
       docEnd=compare; 
      } 
     } 
    } 

    // for each key find an index value 
    for(Iterator<String> iter = set.iterator(); iter.hasNext();){ 
     String key = iter.next();      // the current key value 
     // for each index value map that value adjusting for the length of the original document 
     ArrayList<Integer> toAdd = new ArrayList<Integer>(); 
     for(Iterator<Integer> iter2 = this.find(key).iterator(); iter2.hasNext();){ 
      Integer addIter = iter2.next(); 
      toAdd.add(addIter); // the current index value 
     } 

     /** 
     *Below is the loop in which the error is thrown 
     */ 
     for(Iterator<Integer> iter3 = toAdd.iterator(); iter.hasNext();){ 

      Integer addIter = iter3.next();  // The error is thrown on this line 

      map.get(key).add(addIter+docEnd); 
     } 
    } 
} 

我在做什么错?

回答

3

有问题的循环中的循环条件应为iter3.hasNext(),而不是iter.hasNext()

4

Louis Wasserman已经钉了它。

我只想指出,如果您使用了“新”Java for循环语法,您的代码将会简单得多,并且不会(无法!!)在第一名。例如,这里有大约您的代码将看起来像“新” for语法什么:

... 
    Integer docEnd = 0; 
    Set<String> set = map.keySet(); 
    for (String key : set) { 
     for (Integer compare : this.find(key)) { 
      if (compare < docEnd){ 
        docEnd = compare; 
      } 
     } 
    } 

    for (String key : set) { 
     ArrayList<Integer> toAdd = new ArrayList<Integer>(); 
     for (String add : this.find(key)) { 
      toAdd.add(add); 
     } 
     for (Integer add : toAdd) { 
      map.get(key).add(add * docEnd); 
     } 
    } 

是不是这么多可读性?


我把“新”在引号,因为这个语法是在Java 5中,其是在2004年发布它应该是所有执业Java程序员的标准reportoire的部分介绍...和教师通过...现在。

请不要复制并粘贴上面的代码。这只是为了说明我的观点。

+0

+1因为导师应该跟上! – Fuhrmanator 2012-04-21 01:45:38

+0

@Fuhrmanator - 公平地说,我们不知道Ocasta的教练是否有过错。 – 2012-04-21 07:25:12

+0

作为一名指导老师,即使在我正式使用它之后,我仍然没有在“新”java :)之后更新我的作业。某处的某位讲师未使用“新的”语法... – Fuhrmanator 2012-04-21 14:15:27