为什么我的代码中出现了nosuchelementexception

时间:2018-02-20 12:41:39

标签: java

为什么我会NoSuchElementException

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class GFG {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        sc.nextLine();
        for (int k = 0; k < t; k++) {
            int n = sc.nextInt();
            int xn = sc.nextInt();
            int yn = sc.nextInt();
            sc.nextLine();
            ArrayList<Integer> l1 = new ArrayList<Integer>(2000);
            ArrayList<Integer> l2 = new ArrayList<Integer>(2000);
            int a[] = new int[2000];
            int b[] = new int[2000];
            for (int i = 0; i < n; i++) {
                a[i] = sc.nextInt();
                l1.add(a[i]);
            }
            sc.nextLine();
            for (int i = 0; i < n; i++) {
                b[i] = sc.nextInt();
                l2.add(b[i]);
            }
            int tip = 0;
            while ((xn > 0) && (yn > 0)&& (!l2.isEmpty())&& (!l1.isEmpty())) {
                int pi = l1.indexOf(Collections.max(l1));
                if (l1.get(pi) >= l2.get(pi)) {
                    tip += l1.get(pi);
                    xn--;
                } else {
                    tip += l2.get(pi);
                    yn--;
                }
                l2.remove(pi);
                l1.remove(pi);

            }
            if (yn > 0) {
                while ((yn > 0) && (!l2.isEmpty())) {
                    tip += Collections.max(l2);
                    l2.remove(l2.indexOf(Collections.max(l2)));
                    yn--;
                }
            } else {
                while ((xn > 0) && (!l1.isEmpty())) {
                    tip += Collections.max(l1);
                    l1.remove(l1.indexOf(Collections.max(l1)));
                    xn--;
                }
            }
            System.out.println(tip);
        }
    }
}

输出:

Runtime Error: Runtime ErrorException in thread "main"
    java.util.NoSuchElementException  at
    java.util.Scanner.throwFor(Scanner.java:862)  at
    java.util.Scanner.next(Scanner.java:1485)     at
    java.util.Scanner.nextInt(Scanner.java:2117)  at
    java.util.Scanner.nextInt(Scanner.java:2076)  at
    GFG.main(File.java:22)

1 个答案:

答案 0 :(得分:0)

我在这里添加了一些日志:

  int tip = 0;
  while ((xn > 0) && (yn > 0)) {
    System.out.println("l1:"+l1);
    System.out.println("Collections.max(l1):"+Collections.max(l1));
    int pi = l1.indexOf(Collections.max(l1));

Entring 1 15 9 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 我得到了一个不同的错误。

打印出来:

l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Collections.max(l1):15
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Collections.max(l1):14
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Collections.max(l1):13
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Collections.max(l1):12
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Collections.max(l1):11
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Collections.max(l1):10
l1:[1, 2, 3, 4, 5, 6, 7, 8, 9]
Collections.max(l1):9
l1:[1, 2, 3, 4, 5, 6, 7, 8]
Collections.max(l1):8
l1:[1, 2, 3, 4, 5, 6, 7]
Collections.max(l1):7
l1:[1, 2, 3, 4, 5, 6]
Collections.max(l1):6
l1:[1, 2, 3, 4, 5]
Collections.max(l1):5
l1:[1, 2, 3, 4]
Collections.max(l1):4
l1:[1, 2, 3]
Collections.max(l1):3
l1:[1, 2]
Collections.max(l1):2
l1:[1]
Collections.max(l1):1
l1:[]
Exception in thread "main" java.util.NoSuchElementException
    at java.util.ArrayList$Itr.next(ArrayList.java:854)
    at java.util.Collections.max(Collections.java:669)
    at GFG.main(GFG.java:31)

问题是你在空列表上调用Collections.max