英文:
Need help in JAVA with ArrayIndexOutOfBoundsExceptions
问题
以下是翻译好的内容:
所以我现在正在上Java课程,我们应该编写一个使用ArrayIndexOutOfBoundsException的程序。很简单,对吧?但出于某种原因,我得到了以下错误:
"错误:不兼容的类型:无法将ArrayIndexOutOfBoundsException转换为Throwable"
import java.util.*; //用于异常处理
class ArrayIndexOutOfBoundsException {
public static void main(String[] args) {
int[] array = fillArray(); //填充数组,包含100个值
int userInput = getInput();
try {
System.out.print("元素 " + userInput + " 的值是 " + array[userInput]);
}
catch (ArrayIndexOutOfBoundsException ex) {
System.out.print("超出边界");
}
}//结束main
public static int[] fillArray(){ //填充数组的函数
int[] array = new int[100];
for (int i = 0; i < 100; i++) {
array[i] = (int)(Math.random() * 100) + 1;
}
return array;
}//结束fillArray
public static int getInput(){ //确保用户输入正确值的函数
System.out.print("请输入要查找相应值的索引:");
Scanner input = new Scanner(System.in); //用于输入
int userInput = 0;
try {
userInput = input.nextInt();
}
catch (InputMismatchException ex) {
System.out.println("\n不是有效的数字\n请重试");
return getInput();
}
return userInput;
} //结束getInput
}//结束类
这是我运行的代码,在 "catch (ArrayIndexOutOfBoundsException ex) {" 处发生错误。
然而,如果我将其更改为IndexOutOfBoundsException,代码将按预期工作,我真的找不出为什么。我的意思是,它应用于数组,所以ArrayIndexOutOfBoundsException不适用吗?至少根据我尝试在Google中搜索问题所得到的信息是这样的...但显然我还遗漏了其他内容。
我在Repl.it上运行它,我不认为这会有什么影响,但谁知道呢?
另外,对于格式不佳的代码,我道歉。由于某种原因,当我复制粘贴它时,格式会变得混乱,所以这是我能够在合理的时间内修复以使其可读的最佳方式。
无论如何,任何帮助/建议/甚至是指引我朝着正确方向的提示都将不胜感激!谢谢!
<details>
<summary>英文:</summary>
so I'm in a Java class right now and we're supposed to write a program using ArrayIndexOutOfBoundsExceptions. Simple enough, right? Well for whatever reason I'm getting the error:
"error: incompatible types: ArrayIndexOutOfBoundsException cannot be converted to Throwable"
import java.util.*; //for exceptions
class ArrayIndexOutOfBoundsException {
public static void main(String[]args){
int[] array = fillArray(); //filling the array with 100 values
int userInput = getInput();
try{
System.out.print("The value of element "+userInput+" is "+array[userInput]);
}
catch (ArrayIndexOutOfBoundsException ex) {
System.out.print("Out of Bounds");
}
}//end main
public static int[] fillArray(){ //function to fill the array
int[] array = new int[100];
for (int i = 0; i<100;i++)
{
array[i] = (int)(Math.random() *100) + 1;
}
return array;
}//end fillArray
public static int getInput(){ //function to make sure the user enters a proper value
System.out.print("Please enter the index value to find the corresponding value at that location: ");
Scanner input = new Scanner(System.in); //for input
int userInput = 0;
try{
userInput = input.nextInt();
}
catch(InputMismatchException ex){
System.out.println("\nNot a valid number\nPlease Try Again");
return getInput();
}
return userInput;
} //end getInput
}//end class
This is the code I'm running, with the error occurring at the "catch (ArrayIndexOutOfBoundsException ex) {".
However, if I switch that to just an IndexOutOfBoundsException the code works as intended and I cannot for the life of my figure out why. I mean, it's being applied to an Array so shouldn't an ArrayIndexOutOfBoundsException apply? At least that's what I've been able to find trying to google the problem...but clearly there is something else I'm missing.
I'm running it on Repl.it, I don't think that would make a difference, but never know?
Also, I apologize for the poorly formatted code, for whatever reason when I copy and paste it, it get's all messed up so this was the best I was able to fix it to make it readable in a reasonable amount of time.
Anyways, any help/advice/so much as pointing me in the right direction would be appreciated! Thank you!
</details>
# 答案1
**得分**: 1
更改你的类名,它就会起作用。这是最佳解决方案,因为它可以阻止对java.lang.ArrayIndexOutOfBoundsException的掩盖:
```java
class ArrayIndexOutOfBoundsExceptionTest {
public static void main(String[] args) {
int[] array = fillArray(); // 填充数组,包含100个值
int userInput = getInput();
try {
System.out.print("元素 " + userInput + " 的值是 " + array[userInput]);
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.print("越界");
}
} // 结束 main
为了完整和演示的目的,在这里我没有重新命名类,而是使用了异常的完整路径名(java.lang):
class ArrayIndexOutOfBoundsException {
public static void main(String[] args) {
int[] array = fillArray(); // 填充数组,包含100个值
int userInput = getInput();
try {
System.out.print("元素 " + userInput + " 的值是 " + array[userInput]);
} catch (java.lang.ArrayIndexOutOfBoundsException ex) {
System.out.print("越界");
}
} // 结束 main
英文:
Change the name of your class and it will work. This is the best solution, as it stops the masking of java.lang.ArrayIndexOutOfBoundsException:
class ArrayIndexOutOfBoundsExceptionTest {
public static void main(String[]args){
int[] array = fillArray(); //filling the array with 100 values
int userInput = getInput();
try{
System.out.print("The value of element "+userInput+" is "+array[userInput]);
}
catch (ArrayIndexOutOfBoundsException ex) {
System.out.print("Out of Bounds");
}
}//end main
For completeness and demonstration purposes, here I haven't renamed the class, but instead used the full pathname (java.lang) for the Exception
class ArrayIndexOutOfBoundsException {
public static void main(String[]args){
int[] array = fillArray(); //filling the array with 100 values
int userInput = getInput();
try{
System.out.print("The value of element "+userInput+" is "+array[userInput]);
}
catch (java.lang.ArrayIndexOutOfBoundsException ex) {
System.out.print("Out of Bounds");
}
}//end main
答案2
得分: -1
你的实现存在几个问题。
- 已经存在一个名为 ArrayIndexOutOfBoundsException 的类。
- 你会在文档中看到该类的层次结构包括 Throwable,而你的类没有包含这一点。
因此,你的类需要扩展 IndexOutOfBoundsException 或 Throwable 类。
英文:
There are a couple of problems with your implementation.
- There is already a class called ArrayIndexOutOfBoundsException.
- You will see in the documentation that the class's hierarchy includes Throwable, which your class does not.
So you need your class to either extend the IndexOutOfBoundsException or the Throwable class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论