Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对于ArrayList_Java_Foreach_Iterator - Fatal编程技术网

Java 对于ArrayList

Java 对于ArrayList,java,foreach,iterator,Java,Foreach,Iterator,下面的类用于按行索引单词的位置。 抛出错误的方法意味着在当前索引上附加单独文档的索引。也就是说,如果第一个文档中有6行,则附加文档的第一行应被索引为第7行 public class DocumentIndex { // a NavigableMap implementation to store indexed words and their locations private TreeMap<String, ArrayList<Integer>> map = n

下面的类用于按行索引单词的位置。 抛出错误的方法意味着在当前索引上附加单独文档的索引。也就是说,如果第一个文档中有6行,则附加文档的第一行应被索引为第7行

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);
        }
    }
}

我做错了什么?

有问题的循环中的循环条件应该是iter3.hasNext,而不是iter.hasNext。

有问题的循环中的循环条件应该是iter3.hasNext,而不是iter.hasNext。

路易斯·瓦瑟曼已经搞定了

我只想指出,如果您使用了新的Java for循环语法,您的代码会简单得多,而且不会!!我一开始就犯了那个错误。例如,下面是使用新For语法时代码的大致情况:

这难道不是更具可读性吗

我在引号中添加了新的内容,因为这种语法是在2004年发布的Java5中引入的。它应该是所有实践Java程序员的标准报告的一部分。。。还有老师。。。到现在为止


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

路易斯·沃瑟曼(Louis Wasserman)已经抓住了这一点

我只想指出,如果您使用了新的Java for循环语法,您的代码会简单得多,而且不会!!我一开始就犯了那个错误。例如,下面是使用新For语法时代码的大致情况:

这难道不是更具可读性吗

我在引号中添加了新的内容,因为这种语法是在2004年发布的Java5中引入的。它应该是所有实践Java程序员的标准报告的一部分。。。还有老师。。。到现在为止


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

@Fuhrmanator-公平地说,我们不知道Ocasta的讲师是否有错。作为一名讲师,我对没有在新java之后更新我的家庭作业感到内疚:即使在正式发布后不久我自己也在使用它。某个地方的一些指导老师没有使用新的语法…@Fuhrmanator-公平地说,我们不知道Ocasta的指导老师是否有错。作为一名指导老师,我对没有在新java之后更新我的家庭作业感到内疚:尽管我自己在正式发布后不久就在使用它。某个地方的讲师没有使用新语法。。。
    ...
    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);
        }
    }