2013-09-23 2 views
-3

는 경계 오류 중

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) 

이 오류가 무엇을 의미합니까 말하는이 오류가? 내 프로그램은 프로그램에서 생성 된 좌표를 다른 파일에 작성하는 것입니다. 프로그램이 컴파일되지만 실행되지 않습니다. 어떻게이 오류를 해결할 수 있습니까?

감사합니다.

을 Heres 코드 :

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 

왜 이런 일이?

+1

우리는 당신이 우리에게 코드를 표시하지 않는 경우 답변에 당신을 도울 수 있지만 오류에서 당신이 액세스하려는 것이 분명하다 정의되지 않은 배열의 위치. 예 :'array [5]'(5 개 요소)를 가지고 있고, 여러분이 볼 수있는 것처럼, 배열 [6]'에 접근하려고 시도하고 있습니다. – Chaos

+1

배열에서 범위를 벗어난 색인. – arshajii

+0

코드를 볼 수 있습니까? –

답변

2

교체 당신은 "차원"배열의 실제 길이가 있는지 확인 할 수 있지만,이 사항 Array.length이

그러나 것을 확신 할 수

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 

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(lengthscale, locations, numpoints, dimension, length); 
,210

는 그러나 이것은 당신이 방법을 정의하는 방법이다 : 당신은 NumPoints와 함께 차원을 교환하는

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

코드를 보지 않고도 배열의 인덱스 2에 액세스하려고 시도하고 있음을 알 수 있습니다. 범위를 벗어 났으므로 배열의 길이는 2 이하 여야합니다. 유효한 배열 인덱스는 0에서 length - 1까지입니다.