超出界限错误

时间:2013-09-23 22:57:50

标签: 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.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 

为什么会这样?

2 个答案:

答案 0 :(得分:2)

替换

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 a = 0; a < lengthscale.length; a++) {// for loop runs while a is less than dimension
  lengthscale[a] = length;// stores array
}// end for loop

您不能确保“dimension”是数组的实际长度,但您可以确定array.length是

然而,错误的根本原因在于你有这个:

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

但这就是你定义方法的方法:

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
...

答案 1 :(得分:0)

即使没有看到您的代码,我也可以告诉您正在尝试访问数组的索引2。因为它超出范围,所以数组的长度必须为2或更短。请记住,有效数组索引为0length - 1