嗨,我正在学习java过去几天只有当我尝试执行一个简单的程序时,我在运行时得到一个ArrayIndexoutofbounds异常请帮助我修复这个异常
这是我的代码
class Armstrong
{
public static void main(String args[])
{
if (args.length!=0) {
System.out.println("value is required");
System.exit(0);
}
int num = Integer.parseInt(args[0]);
int n = num;
int check =0, remainder;
while(num>0)
{
remainder = num%10;
check = check+(int)Math.pow(remainder,3);
num = num/10;
}
if (check==n)
System.out.println(n+ "isa armstrong number");
else
System.out.println(n + "is not a armstrong number");
}
}
Run Code Online (Sandbox Code Playgroud) 我无法弄清楚问题是有人请帮忙.该程序应该找到数组中重复值的索引并打印出来.输出java.lang.ArrayIndexOutOfBoundsException:10.
private static String s = "";
private static int num = 0;
public static void main(String[] args) {
int[] array = { 1, 5, 3, 8, 2, 3, 7, 1, 9, 3 };
for (int i = 1; i <= array.length; ++i) {
while (num <= array.length - 2 && array[num] == array[i]) {
s += i + ",";
num += 1;
}
}
System.out.println("index 0 are at positions" +s);
System.out.println();
}
Run Code Online (Sandbox Code Playgroud) mhey我必须对下面的代码执行以下操作,并且不确定如何正确地将其打印到事件日志溢出 - 终止一次.它的打印次数多次.
要做:使用名为EVENT_MAX ii的常量(最终int)指定数组事件的长度.方法recordEvent检查数组事件中是否还有剩余空间来存储另一个事件(提示:比较xevent和EVENT_MAX).如果没有则消息:事件日志溢出 - 应该输出终止并且程序以下面的方法调用终止:System.exit(1);
class RecordEvents2 {
public static void main (String args[]) {
Recorder r1 = new Recorder (100,100,"Wombat Detection");
r1.recordEvent("10:53");
r1.recordEvent("10:59");
r1.recordEvent("11:05");
r1.recordEvent("12:59");
r1.recordEvent("13:50");
r1.recordEvent("14:06");
r1.printEvents();
}
}
class Recorder {
int xPos,yPos;
String eventType;
String [] event = new String [5];
final int EVENT_Max = 0;
int xevent = 0;
Recorder (int xPos, int yPos, String eventType ) {
this.xPos = xPos;
this.yPos = yPos ;
this.eventType = eventType;
}
void recordEvent (String eventTime …Run Code Online (Sandbox Code Playgroud)public class Stringreverser {
public char[] reverse(String a){
char[] arre = a.toCharArray() ;
char[] result = {} ;
int count = 0 ;
for(int inarre = arre.length -1 ; inarre >= 0 ; inarre = inarre -1 ){
result[count] = arre[inarre] ;
count+= 1 ;
}
return result ;
}
public static void main(String argv[]){
System.out.println(new Stringreverser().reverse("hello")) ;
}
}
Run Code Online (Sandbox Code Playgroud)
这一直给我一个 -Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0错误。该程序应该接收一个字符串并反转并返回一个字符数组。问题似乎出在result[count] = arre[inarre] ;. 问题是什么 ?
如果你的问题是我java.lang.ArrayIndexOutOfBoundsException在我的代码中得到了一个,我不明白为什么会发生这种情况.它是什么意思,我该如何避免它?
这是关于这个主题的最全面的Canonical信息集合
java.lang.ArrayIndexOutOfBoundsException以及java.lang.IndexOutOfBoundsException.
有很多像这样的问题,所有这些都有模糊的没有代码答案,或者大多数都非常具体,并且局限于手头的问题,并没有解决在所有情况下完全相同的根本原因.
如果您看到属于此一般情况的一个,而不是使用更多重复的专门内容回答它,请将其标记为此副本的副本.