Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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_Arrays_Methods - Fatal编程技术网

Java 越界误差

Java 越界误差,java,arrays,methods,Java,Arrays,Methods,我听到这个错误说 java.lang.ArrayIndexOutOfBoundsException: 2 at J4.writefile(J4_2_MultiDimensionalArray7.java:30) at J4.main(J4_2_MultiDimensionalArray7.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.Na

我听到这个错误说

java.lang.ArrayIndexOutOfBoundsException: 2
    at J4.writefile(J4_2_MultiDimensionalArray7.java:30)
    at J4.main(J4_2_MultiDimensionalArray7.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
这个错误意味着什么?我的程序是将程序生成的坐标写入另一个文件。 程序可以编译,但不能运行。 如何修复此错误

谢谢

代码如下:

import java.io.*;
import java.util.Arrays;

public class J4
{
    public static void main (String [] args) throws IOException
    {
        int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100

        //arrays are initializewd and declared
        double [] lengthscale = new double [dimension];
        double [][] locations = new double [numpoints][dimension];

        PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));

        writefile(lengthscale, locations, numpoints, dimension, length);


        for(int m=0; m <length; m++){//for loop
            fileOut.println(Arrays.toString(locations[m]) + ", ");//writes to file
        }
        fileOut.close ();//close file

    }//end main

    public static Double writefile(double lengthscale[], double locations[][], int dimension, int numpoints, int length)throws IOException
    {

        for (int a = 0; a < dimension; a++){//for loop runs while a is less than dimension
            lengthscale[a] = length;//stores array
        }//end for loop

        for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
            for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
                locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within 
                return locations[x][y];

            }//end nested for loop
        }//end for loop

        //if program doesnt run through loop.. alternative return statement (but
        double b= 1;    
        return b;
    }//end writefile methos
}//end 
import java.io.*;
导入java.util.array;
公共类J4
{
公共静态void main(字符串[]args)引发IOException
{
int numpoints=100,维度=2,长度=100;//numpoints设置为100,维度设置为2,长度设置为100
//数组被初始化并声明
双[]长度刻度=新双[尺寸];
双精度[][]位置=新的双精度[numpoints][dimension];
PrintWriter fileOut=新的PrintWriter(新的FileWriter(“arrayNumPoints.txt”);
writefile(长度比例、位置、点数、尺寸、长度);

对于(int m=0;m即使没有看到您的代码,我也可以看出您正在尝试访问数组的索引
2
。因为它超出了范围,数组的长度必须小于等于
2
。请记住,有效的数组索引是
0
length-1

for (int a = 0; a < dimension; a++) {// for loop runs while a is less than dimension
  lengthscale[a] = length;// stores array
}// end for loop
但这就是您定义方法的方式:

writefile(double lengthscale[], double locations[][], int dimension,
      int numpoints, int length)
您正在用numpoints交换维度

您可以将代码替换为:

..
    PrintWriter fileOut = new PrintWriter(new FileWriter("arrayNumPoints.txt"));
    writefile(lengthscale, locations, dimension, numpoints, length);
    for (int m = 0; m < length; m++) {// for loop
...
。。
PrintWriter fileOut=新的PrintWriter(新的FileWriter(“arrayNumPoints.txt”);
writefile(长度比例、位置、尺寸、小数点、长度);
for(int m=0;m
如果您不向我们显示代码,我们无法帮助您提供答案,但从错误中可以清楚地看出,您正在尝试访问尚未定义的数组中的某个位置。例如:您有
数组[5]
(5个元素),并且您正在尝试访问
数组[6]
正如您所看到的,这是越界的,因为您试图以越界索引访问数组。我们可以看到您的代码吗?(另外,指出哪一行是第30行。)第一行表示这一切
ArrayIndexOutOfBoundsException
之所以发生这种情况,是因为您正在调用一个超出边界的索引,假设您有一个3大小的数组,并且您调用了第四个元素。
writefile(double lengthscale[], double locations[][], int dimension,
      int numpoints, int length)
..
    PrintWriter fileOut = new PrintWriter(new FileWriter("arrayNumPoints.txt"));
    writefile(lengthscale, locations, dimension, numpoints, length);
    for (int m = 0; m < length; m++) {// for loop
...