Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java错误:线程中出现异常;“主要”;java.lang.ArrayIndexOutOfBoundsException_Java - Fatal编程技术网

Java错误:线程中出现异常;“主要”;java.lang.ArrayIndexOutOfBoundsException

Java错误:线程中出现异常;“主要”;java.lang.ArrayIndexOutOfBoundsException,java,Java,我是一名java初学者,正在学习普林斯顿大学提供的算法课程。我遵循第9页的一本书的例子:BinarySearch import edu.princeton.cs.algs4.*; import java.util.Arrays; public class BinarySearch { public static int rank(int key, int[] a) { int lo = 0; int hi = a.length - 1;

我是一名java初学者,正在学习普林斯顿大学提供的算法课程。我遵循第9页的一本书的例子:BinarySearch

import edu.princeton.cs.algs4.*;
import java.util.Arrays;

public class BinarySearch 
{
    public static int rank(int key, int[] a)
    {
        int lo = 0;
        int hi = a.length - 1;
    while (lo <= hi)
    {
        int mid = lo + (hi - lo) / 2;
        if      (key < a[mid]) hi = mid - 1;
        else if (key > a[mid]) lo = mid + 1;
        else return mid;
    }
    return -1;
}

    public static void main(String[] args) 
    {
        In in = new In(args[0]);
        int[] whitelist = in.readAllInts();
        Arrays.sort(whitelist);

        while (!StdIn.isEmpty()) 
        {
            int key = StdIn.readInt();
            if (BinarySearch.rank(key, whitelist) == -1)
            StdOut.println(key);
    }
}

我认为没有程序错误。任何建议都是有用的。谢谢。

您的
main
方法要求在启动程序时提供命令行参数

In in = new In(args[0]);
ArrayIndexOutOfBoundsException表示您试图访问数组外部的元素(即不存在的元素)。在此,您尝试通过以下线路访问它:

In in = new In(args[0]);

因此,您需要修复对程序的输入,因为它可能会显示args[]数组没有被填充。

我的答案只是在这里澄清一些问题。把这个作为另外两个的补充

什么是
阵列索引边界外异常

官方定义见此:

我的定义:

ArrayIndexOutOfBounds
基本上就是它的发音。在java中,数组是静态初始化的(大部分时间)。创建后,会为其分配一定数量的元素。您不能尝试在元素中存储大于已分配数组的内容的内容。例如:

int[] nums = new int[1]; //allocate nums to have only 1 element (remember indexing starts at 0)
nums[1] = 0;  //this will throw Array index OOB exception
为什么代码抛出
ArrayIndexOutOfBoundsException

这是因为java运行
main()
方法,该方法运行其余代码。Main接受名为
args[]
String
数组的参数。此数组的内容也称为命令行参数。当您通过cmd运行代码时,需要指定
args[]
,以便它像为任何其他函数调用指定参数一样使用。记住,java索引从0开始,所以您给出的第一个参数是0,第二个参数是1,等等。因为您根本没有指定任何参数,所以您访问的是不存在的元素。下面是一个正确运行的示例:(我相信没有引用)

现在在代码中如果我使用

 System.out.println(args[0]);
我将获得“argument0”


如果您使用的是像Eclipse这样的IDE,这可能有点不同。特别是对于Eclipse,单击“运行”菜单,然后单击“运行配置”。然后单击参数选项卡,然后指定参数。对于多个参数,请使用逗号分隔

在哪一行有错误?您在问什么是数组索引越界异常?还是你在问是什么原因?或者两者都有?另外,@Abdelhak错误是一致的22@Abdelhak它可以生成一个java类。所以我认为每一行都没有错…@AshwinGupta这两行我都想知道。谢谢你指出这一点。@Gaosinge好的,peter和thilo介绍了你的解决方案。我来解释数组异常
ArrayIndexOutOfBounds
基本上就是它听起来的样子。在java中,数组是静态初始化的(大部分时间)。创建后,会为其分配一定数量的元素。您不能尝试在元素中存储大于已分配数组的内容的内容。例如:
int[]bob=newint[1]如果我尝试
bob[1]=3之后,我将获得异常。如何在CMD中修复程序的输入?你能举个例子吗?@Gaosinge看看我在Thilo的答案上发表的评论。@Gaosinge另外,为了澄清一下,记住,java运行main()方法,该方法运行其余代码。Main接受名为args[]的字符串数组的参数。此数组的内容也称为命令行参数。通过cmd运行代码时,需要指定args[],以便使用。请记住,java索引从0开始,因此您给出的第一个参数是0,第二个参数是1,等等。下面是一个运行示例,
java myProgram.class“argument0”,“argument1”
。现在在代码中,如果我使用
System.out.println(args[0])我将获得
参数0
int[] nums = new int[1]; //allocate nums to have only 1 element (remember indexing starts at 0)
nums[1] = 0;  //this will throw Array index OOB exception
java myProgram.class "argument0", "argument1"
 System.out.println(args[0]);