LinkedList:Collections.max()抛出NoSuchElementException

时间:2014-10-22 18:44:06

标签: java collections nosuchelementexception

我没有通过扫描仪或其他方法迭代LinkedList,我使用Collections.max()LinkedList获取最大数量。

我已经在Stack Overflow上读到由于迭代器或扫描器或标记器而引发此异常,但我没有使用它们。

import java.io.*;
import java.util.*;

class TLG {
    public static void main(String[] args)throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        LinkedList<Integer> first = new LinkedList<Integer>();
        LinkedList<Integer> second = new LinkedList<Integer>();

        int cases = Integer.parseInt(br.readLine());

        for(int i=1;i<=cases;i++) {
            String score = br.readLine();
            int number1 = Integer.parseInt(score.split(" ")[0]); 
            int number2 = Integer.parseInt(score.split(" ")[1]); 
            int diff = number1 - number2;

            if(diff > 0){
                first.add(diff);    
            }
            else {
                second.add(java.lang.Math.abs(diff));    
            }
        }

        Integer max1 = Collections.max(first);  // Getting Exception here
        Integer max2 = Collections.max(second); // Getting Exception here

        if(max1 > max2) {
            System.out.println(1+" "+max1);
        }
        else {
            System.out.println(2+" "+max2);
        }
    }
}

3 个答案:

答案 0 :(得分:4)

你用空列表调用Collections.max()。

答案 1 :(得分:3)

/**
 * Returns the maximum element of the given collection, according to the
 * <i>natural ordering</i> of its elements.  All elements in the
 * collection must implement the <tt>Comparable</tt> interface.
 * Furthermore, all elements in the collection must be <i>mutually
 * comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a
 * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
 * <tt>e2</tt> in the collection).<p>
 *
 * This method iterates over the entire collection, hence it requires
 * time proportional to the size of the collection.
 *
 * @param  coll the collection whose maximum element is to be determined.
 * @return the maximum element of the given collection, according
 *         to the <i>natural ordering</i> of its elements.
 * @throws ClassCastException if the collection contains elements that are
 *         not <i>mutually comparable</i> (for example, strings and
 *         integers).
 * @throws NoSuchElementException if the collection is empty. <---------------
 * @see Comparable
 */
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

答案 2 :(得分:1)

您没有检查空案例:因此Collections.max()会抛出NoSuchElementException

首先检查任何列表是否为空(然后您知道最大值以及提供该列表的列表)

    Integer max;
    int list;
    if (first.isEmpty()) {
        max = Collections.max(second);
        list = 2;
    } else if (second.isEmpty()) {
        max = Collections.max(first);
        list = 1;
    } else {
        Integer max1 = Collections.max(first);
        Integer max2 = Collections.max(second);
        if (max1 > max2) {
            max = max1;
            list = 1;
        } else {
            max = max2;
            list = 2;
        }
    }
    System.out.println(list + " " + max);