【问题标题】:I am getting a runtime exception and i do not know what's causing it我得到一个运行时异常,我不知道是什么原因造成的
【发布时间】:2020-04-22 15:32:25
【问题描述】:

我正在使用在线 ide geeksforgeeks。在这里,我试图使用 TreeSet 并传递一个 Comparator 对象来解决这个问题。问题如下:

给定一个大小为 N 的数组 A。用数组中的下一个最大元素(其右侧最大的元素)替换每个元素。另外,由于最后一个元素旁边没有元素,因此将其替换为 -1。

对 t 个测试用例执行此操作: 这是我写的代码:

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

class GFG {
    static Scanner sc=new Scanner(System.in);
    public static void main (String[] args) {
        //code
        int t=sc.nextInt();
            for(int i=0;i<t;i++)
                display();
    }
    static void display(){
        int n=sc.nextInt();
        Set<Integer> ts=new TreeSet<Integer>(new myComparator());
        int a;
        for(int i=0;i<n;i++){
            a=sc.nextInt();
            ts.add(a);
        }
        Iterator itr=ts.iterator();
        int count=0;
        while(itr.hasNext()){
            if(count==0)
                continue;
            else{
                System.out.print(itr.next()+" ");
            }
        }
        System.out.print(-1);
        System.out.println();
    }
}
class myComparator implements Comparator<Integer>{
    public int compare(Integer obj1,Integer obj2){
        if(obj2>obj1)
            return 1;
        else if(obj2<obj1)
            return -1;
        else 
            return 0;
    }
}

我的代码抛出的错误:

Exception 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.display(File.java:20)
    at GFG.main(File.java:13)

请更正代码。 Code

【问题讨论】:

标签: java runtime-error comparator treeset nosuchelementexception


【解决方案1】:

这是因为 display() 中的 for 循环迭代到 n 这是元素的值,而不是找到的元素数。

所以在你的代码中

int n=sc.nextInt();

'n' 变为值 887

for(int i=0;i<n;i++){

迭代 84 次(传入的元素数),然后抛出此异常,因为它不能再进一步(试图到达 887)。因此

java.util.NoSuchElementException

因为没有更多元素可以使用nextInt()

【讨论】:

    【解决方案2】:

    java.util.NoSuchElementException只有在输入控制台耗尽时才有可能。

    除此之外,if(count==0) 在上述代码中始终为真,并且永远不会执行 else 块。

    【讨论】:

      【解决方案3】:

      无法重现 NoSuchElementException,但是,您的代码包含无限循环,因为您从不使用迭代器中的元素:

      while(itr.hasNext()){
          if(count==0)
              continue;
          else {
             System.out.print(itr.next()+" ");
          }
      }
      

      假设这个问题解决了,代码运行起来,还有其他问题:

      • 使用 Set 会导致输入数据丢失
      • 使用反向比较器更改输出顺序
      • 您的代码似乎没有生成包含 -1 的输出数组

      在应用这些修复和测试时

      Iterator itr=ts.iterator();
      int count=0;
      while(itr.hasNext()){
          if (count == 0) {
              itr.next();
              count++;
          } else {
              System.out.print(itr.next() + " ");
          }
      }
      System.out.print(-1);
      System.out.println("\nEND" + ts);
      

      已检索到以下结果:

      input n:

      5

      20 20 30 40 40

      30 20 -1
      END[40, 30, 20]
      

      【讨论】:

        猜你喜欢
        • 2018-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-30
        相关资源
        最近更新 更多