Java 如何避免数组索引超出边界?
我试图输入一个数字,并让程序创建一个新的数组的长度。然后,我将一次一个地输入数组中的所有元素。然后我键入一个数字,程序将在数组中搜索该数字。不幸的是,我在下面编写的代码抛出了一个Java 如何避免数组索引超出边界?,java,arrays,Java,Arrays,我试图输入一个数字,并让程序创建一个新的数组的长度。然后,我将一次一个地输入数组中的所有元素。然后我键入一个数字,程序将在数组中搜索该数字。不幸的是,我在下面编写的代码抛出了一个ArrayIndexOutOfBoundsException。我怎样才能解决这个问题 import java.io.*; public class aw { public static void main(String args [])throws IOException`` { Buff
ArrayIndexOutOfBoundsException
。我怎样才能解决这个问题
import java.io.*;
public class aw
{
public static void main(String args [])throws IOException``
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int z;
boolean vince = false;
System.out.print("Enter Element :");
int a = Integer.parseInt(in.readLine());
int [] jes = new int[a];
for(z=0; z<jes.length; z++)
{
System.out.print("Numbers :");
jes[z] = Integer.parseInt(in.readLine());
}
System.out.print("Enter search:");
int []x = new int [100];
x[100] = Integer.parseInt(in.readLine());
if(jes[z] == x[100])
{
vince = true;
if(vince == true)
{
System.out.print("Array "+jes[z]+ "Found at Index"+z); // here is my problem if i input numbers here it will out of bounds
}
}
}
}
import java.io.*;
公开课
{
公共静态void main(字符串args[])引发IOException``
{
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
intz;
布尔文斯=假;
系统输出打印(“输入元素:”);
inta=Integer.parseInt(in.readLine());
int[]jes=新的int[a];
对于(z=0;z第一个问题是您在不重置z的情况下重用了z。您的循环增加z,直到它大于数组的最大索引jes
,因此,当您尝试重用z时,您使用的索引超出了范围。我认为您在尝试将读入值与jes
哪个c进行比较时缺少for循环ould可能会重置和重用z,但使用不同的变量来递增可能会更清楚
第二个问题是,您为x声明了一个大小为100的数组,并且正在尝试访问第101个索引(超出范围)。int[]x=new int[100]
的标记为0-99
此代码应按照您的预期工作:
import java.io.*;
public class aw
{
public static void main(String args []) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
boolean vince = false;
System.out.print("Enter Element :");
int a = Integer.parseInt(in.readLine());
int [] jes = new int[a];
for(int i=0; i<jes.length; i++) {
System.out.print("Numbers :");
jes[i] = Integer.parseInt(in.readLine());
}
System.out.print("Enter search:");
int x = Integer.parseInt(in.readLine());
for(int i=0; i < jes.length; i++) {
if(jes[i] == x) {
vince = true;
break; //found the value, no need to keep iterating
}
}
if(vince == true) {
System.out.print("Array "+jes[i]+ "Found at Index"+i);
}
}
}
import java.io.*;
公开课
{
公共静态void main(字符串args[])引发IOException{
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
布尔文斯=假;
系统输出打印(“输入元素:”);
inta=Integer.parseInt(in.readLine());
int[]jes=新的int[a];
对于(int i=0;i我不确定您想做什么,但当您尝试访问x[100]时,会出现ArrayOutOfBoundsException。在该上下文中,x是一个包含100个元素的数组,但在java中,x[0]是第一个元素,因此x的最后一个元素是x[99],x[100]是第101个元素…超出范围!我想你的意思是ArrayIndexOutOfBounds
在哪一行出现异常?在我的最后一行,先生。我在这里发表了一条评论-[CoderShei]你想用你的代码实现什么,因为这个代码有几个问题…不仅仅是这个x[100]=Integer.parseInt(in.readLine());
这实际上会给您带来错误。实际上它被设置为x[100],因为我认为如果使用int x=Integer.parseInt(in.readLine());是导致ArrayIndexOutOfBounce的原因,所以我尝试了x[100]但它不起作用。问题是,如果我输入3个元素,它会显示为输入3个数组,比如说我输入了2,4,6。那么我的数组是{2,4,6}对吗?下一步将询问您的搜索编号是什么,如果我搜索示例2,则输出是ArrayIndexOutOfBounce,而不是在索引0[:3]处找到的数组2好的,如果我理解正确的话,下面是你想要的:首先用户写数组的长度(3),然后他写数组的元素(2,4,6),然后他写他要找的元素(2),然后应用程序说“在索引0处找到元素2”.我的第一次搜索是唯一一个处理int x=integer.parseInt(in.readLine())的整数;但我认为这是数组跳出的原因,所以我将其更改为int[]x=new int[100]。如果你只是在搜索一个数字,你根本不必把它放入数组。@CoderShei我已经把那部分编辑掉了,因为它是不必要的。我明白了,先生,我将把我的x草拟成一个整数而不是数组。非常感谢上帝保佑先生:)!你不需要在If(jes[I]==x[100]行中的[100]
){
@CoderShei如果这回答了您的问题,您应该单击此答案旁边的复选标记。