java - ArraysOutOfBoundException -
i can't find what's wrong code method.it supposed generate 1d double array, random number of elements, , of random values between 2 random integers. keepgetting arrayindexoutofboundexception.
public class array1doperations { public static void main(string[] args) { int n=0; int min=0; int max=0; generate1d(n,min, max); } public static double[] generate1d(int n, int min, int max){ n=(int)(math.random()*10); int x=(int)(math.random()*10); int y=(int)(math.random()*10); min=0; max=0; if (x>y){ x=max; y=min; } else if(y>x){ y=max; x=min; } else if(x==y){ x=min; y+=(int)(math.random()*10); } double[] a=new double[n]; for(int i=0; i<=n; i++){ double random=math.random()*(max-min)+min; a[i]=random; } return a; } }
for(int i=0; i<=n; i++){
you're looping past end of array. last valid index n - 1
, use <
, not <=
.
Comments
Post a Comment