`java.util.NoSuchElementException`和`ArrayIndexOutOfBoundsException`在Java中有什么区别?

huangapple go评论73阅读模式
英文:

What is the difference between java.util.NoSuchElementException and ArrayIndexOutOfBoundsException in Java?

问题

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {
   
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        int l = n * m;
        int arr[] = new int[l];
        int max = -1;
        int c = 0;
        
        for (int i = 0; i < l; i++) {
            int qs = sc.nextInt() - 1;
            int qe = sc.nextInt() - 1;
            int k = sc.nextInt();
            
            if (i >= qs + c) {
                arr[i] = arr[i - c] + k;
                
                if (i == qe + c) {
                    c = c + n;
                    continue;
                }
            }
        }

        for (int i = l - n; i < l; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println(max);
    }
}

问题是如何解决这个错误,以及上述错误之间的区别?

java.util.NoSuchElementException

样本输入:

5 3
1 2 100
2 5 100
3 4 100

样本输出:

200

解释:
在第一次更新后,列表将变为:100 100 0 0 0。
在第二次更新后,列表将变为:100 200 100 100 100。
在第三次更新后,列表将变为:100 200 200 200 100。
所需答案为200。

英文:

I was solving a problem in Hackerrank when the compiler showed this error "java.util.NoSuchElementException"
Problem :Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {  
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int m = sc.nextInt();
int l=n*m;
int arr[]=new int[l];
int max=-1;
int c=0;
for(int i=0;i&lt;l;i++)
{
int qs=sc.nextInt()-1;
int qe=sc.nextInt()-1;
int k=sc.nextInt();
if(i&gt;=qs+c)
{
arr[i]=arr[i-c]+k;
if(i==qe+c)
{
c=c+n;
continue;
}
}
}
for(int i=l-n;i&lt;l;i++)
{
if(arr[i]&gt;max)
{
max=arr[i];
}
}
System.out.println(max);
}
}

My question is how to resolve the error and whats the difference between the above mentioned errors?
Sample input:

5 3
1 2 100
2 5 100
3 4 100
Sample ouput:
200
Explanation
After the first update list will be 100 100 0 0 0.
After the second update list will be 100 200 100 100 100.
After the third update list will be 100 200 200 200 100.
The required answer will be 

答案1

得分: 1

代码部分不要翻译,只返回翻译好的部分:

不同之处在于某些代码抛出一种异常,而其他代码抛出另一种异常。

ArrayIndexOutOfBoundsException 的主要用途如其名称所示,用于在使用错误的数组索引时使用。

相比之下,NoSuchElementException 例如在 Java 集合中用于指示所请求的元素不存在。在你的情况下,很可能是你对 Scanner 上的 nextInt() 的调用出了问题。详见文档页面。

教训:在调用“别人的”函数时,阅读文档以了解其功能。

你实际的问题在于循环期望有 l=n*m(=15) 行 3 个整数,然而实际上只有 3 行这样的数据。看起来你应该循环 3 次。但是我不确定值 5 应该起什么作用。

英文:

The difference is that some code throws one exception and other code throws the other one.

The primary use of ArrayIndexOutOfBoundsException is, as its name suggests, used when using a bad array index.

NoSuchElementException, by contrast, is used for example in Java collections to indicate that a requested element does not exist. In your case, it's likely that your call to nextInt() on the Scanner is responsible. See the documentation page.

Moral: when calling "someone else's" function, read the documentation to find out what it does.

Your actual problem is that you loop expecting l=n*m (=15) rows of 3 integers, whereas there are only 3 such rows. Looks like you should be looping for 3. But I'm not sure what the function of the value 5 is supposed to be.

huangapple
  • 本文由 发表于 2020年9月2日 02:33:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63693527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定