【问题标题】:LinkedList : Collections.max() throwing NoSuchElementExceptionLinkedList : Collections.max() 抛出 NoSuchElementException
【发布时间】:2014-10-22 18:44:05
【问题描述】:

我没有通过扫描仪或其他方法等任何方式迭代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);
        }
    }
}

【问题讨论】:

  • firstsecond 中是否有任何元素?
  • cases的值是多少?
  • 您能否提供一些导致此异常的输入数据示例,您认为不应该这样做?
  • 另外,你不能在两行都有异常,要么在第一行要么在第二行。

标签: java collections nosuchelementexception


【解决方案1】:
/**
 * 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)

【讨论】:

  • 您在答案中发布的内容中至少有 95% 是不重要的噪音
  • 但是我的链接列表不是空的,我删除了 Collections.max() 行并打印了找到的链接列表:5 5 6:它们都不是空的.. 10 20 20 10 6 5 22 65第一个链表元素:[10, 1] 第二个链表元素:[1, 10, 43]
  • @Cyber​​neticTwerkGuruOrc 他以牺牲答案的质量为代价提出了观点。
  • @SamIam 引用相关方法的JavaDoc有什么问题吗?
  • @PankajKushwaha 那么这个异常不会发生。请尝试调试您的程序,看看会发生什么。
【解决方案2】:

您正在使用空列表调用 Collections.max()。

【讨论】:

    【解决方案3】:

    您没有检查空情况:所以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);
    

    【讨论】:

    • 你只是把问题隐藏在那里。真正的问题是为什么这些列表是空的?
    • @user2336315 不是真的,如果int diff = number1 - number2; 总是积极的呢?那么diff 将永远在第一个列表中
    猜你喜欢
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多