有人能告诉我为什么这个java程序显示“数组索引超出界限”例外? [英] Can someone tell me why this java program shows "array index out of bounds" exception ?
问题描述
当我运行它时,我得到了该程序的输出但是我在执行时遇到线程中的异常主java.lang.ArrayIndexOutOfBoundsException:错误。
有人可以告诉我程序出了什么问题吗?
问题:打印数组的Java程序在钟摆安排。
TEST CASE
INPUT:
5 //数组中没有元素
1
2
3
4
>
5
输出:
1 2 3 4 5 //阵列中的元素
5 3 1 2 4 //阵列的摆锤
我尝试了什么:
import java.io. *;
import java.util。*;
import java.lang。*;
public class TestClass
{
public static void main( String [] args) throws IOException
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a [] = new INT [N];
int b [] = new int < /跨度> [N];
for ( int i = 0; i< n; i ++)
{
a [i] = in.nextInt();
}
int t = 0;
for ( int i = 0; i< n; i ++)
{
for ( int j = i + 1; j< n; j ++)
{
if (a [i]> a [j])
{
t = a [i];
a [i] = a [j];
a [j] = t;
}
}
}
for ( int i = 0; i< n; i ++)
{
System.out.print(a [i] + < span class =code-string>);
}
int mid =(n- 1 )/ 2;
int x = 1 ,lim = n- 1 跨度> -mid;
b [mid] = a [ 0 ];
for ( int i = 1; i< = lim; i ++)
{
if ((mid + i)< n)
b [mid + i] = a [x ++];
if ((mid- 1 )> = 0)
b [mid -i] = a [x ++];
}
System.out.print( \ n);
for ( int i = 0; i< n; i ++)
{
System.out.print(b [i] + );
}}
}
你的代码有几个for循环。
所有主题都使用本地索引变量访问数组项,但索引边界看起来是正确的。
我看到问题的唯一循环是访问 b 两次。
仔细观察我看到什么似乎是错字;完整的循环是:
for ( int i = 1; i< = lim; i ++)
{
if ((mid + i)< n)
b [mid + i] = a [x ++];
if ((mid- 1 )> = 0)
b [mid -i] = a [x ++];
}
第二个if可能是问题;
应该是:
if ((mid-i)> = 0)
代替of:
if ((mid- 1 跨度>)GT; = 0)
I got the output for this program when I ran it but I'm getting "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: " error at the time of execution.
Can somebody tell me what went wrong with my program?
QUESTION: Java program to print an array in Pendulum Arrangement.
TEST CASE
INPUT:
5 //no of elements in an array
1
2
3
4
5
OUTPUT:
1 2 3 4 5 //elements in the array
5 3 1 2 4 //pendulum arrangement of the array
What I have tried:
import java.io.*;
import java.util.*;
import java.lang.*;
public class TestClass
{
public static void main(String[] args)throws IOException
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
int b[] = new int[n];
for(int i =0; i<n; i++)
{
a[i] = in.nextInt();
}
int t=0;
for (int i=0; i<n ; i++)
{
for (int j =i+1; j<n; j++)
{
if (a[i] > a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(int i =0; i<n; i++)
{
System.out.print(a[i]+" ");
}
int mid = (n-1)/2;
int x = 1, lim = n-1-mid;
b[mid] = a[0];
for (int i=1; i<=lim; i++)
{
if((mid+i)<n)
b[mid+i] = a[x++];
if((mid-1)>=0)
b[mid-i] = a[x++];
}
System.out.print("\n");
for (int i=0; i<n; i++)
{
System.out.print(b[i]+" ");
}}
}
Your code has several for loops.
All of theme access the array items using the local index variable, but the index bounds seem correct.
The only loop where I see an issue is the one accessing b twice.
Looking carefully I see what seems a typo; the full loop is:
for (int i=1; i<=lim; i++) { if((mid+i)<n) b[mid+i] = a[x++]; if((mid-1)>=0) b[mid-i] = a[x++]; }
The second if is likely to be the issue;
it should be:
if((mid-i)>=0)
instead of:
if((mid-1)>=0)
这篇关于有人能告诉我为什么这个java程序显示“数组索引超出界限”例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!