2012-04-21 2 views
1

아래 클래스는 줄마다 단어의 위치를 ​​색인화합니다. 오류를 발생시키는 방법은 현재 색인에 별도의 문서 색인을 추가하는 것입니다. 즉, 첫 번째 문서의 6 개 라인이있는 경우, 첨부 문서의 첫 번째 라인은 내가 잘못 뭐하는 거지 라인 7.ArrayList가있는 각 루프에 NoSuchElementException이 발생했습니다. <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

문제가있는 루프의 루프 조건은 iter.hasNext()이 아니라 iter3.hasNext()이어야합니다.

4

루이 와서 만 (Louis Wasserman)이 그것을 손톱에 넣었습니다.

"새로운"Java 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); 
     } 
    } 

가 훨씬 더 읽을 수 없습니다 : 예를 들어, 다음 코드는 "새로운"for 구문처럼 보일 것입니다 무슨 약입니까?


이 구문은 그것은 모든 연습 자바 프로그래머의 표준 reportoire의 일부가되어야 2004 년에 출시했다 자바 5에 도입 된 때문에 따옴표에서 "새"를 넣어

... 그리고 강사 ...에 의해 지금.

위 코드를 복사해서 붙여 넣지 마십시오. 그것은 나의 요점을 설명하기위한 것입니다.

+0

강사가 지켜야하기 때문에 +1! – Fuhrmanator

+0

@Fuhrmanator - 공정하기 위해서, 우리는 오케 스타의 강사가 잘못한 것인지 알 수 없습니다. –

+0

강사로서 나는 공식적인 직후에 그것을 사용하고 있었지만 "새로운"java 다음에 숙제를 업데이트하지 않았다고 유죄를 나타 냈습니다. 어딘가에있는 일부 강사는 구문에 "새"를 사용하지 않습니다 ... – Fuhrmanator

관련 문제