我收到的错误信息位于此行“radiusToSelect [p *(p-1)/ 2 + q] = distanceBetweenParticles [p] [q];”,但我无法弄清楚为什么它是错误的。
java代码如下,非常感谢您的帮助。
这是错误消息“2.04.02.06.04.02.08.0线程中的异常”主“java.lang.ArrayIndexOutOfBoundsException:6 在javaAndR.soswarm.main(soswarm.java:59)“
public static void main(String[]args)
{
double [][] score=new double [40][4];
double [][] id=new double [1000][4];
int row, column;
int [][] bmu=new int[id.length][1];
double [][] shortestdistance=new double [id.length][1];
double [][] distance=new double[id.length][score.length];
double [][] distanceBetweenParticles= new double[score.length][score.length];
double[] radiusToSelect=new double [(score[1].length-1)*(score[1].length)/2];
for(row=0;row<score.length;row++)
{
for(column=0;column<score[row].length;column++)
{
score[row][column]=row+column;
}
System.out.print("\n");
}
for (int i =0;i<id.length;i++)
{
for(int j=0;j<id[row].length;j++)
{
id[i][j]=i*j;
//System.out.print(id[i][j]+" ");
}
//System.out.print("\n");
}
for (int p=1;p<score.length;p++)
{
for(int q=0;q<p;q++)
{
distanceBetweenParticles[p][q]=distance(score[p][0],score[p][1],score[p][2],score[p][3],score[q][0],score[q][1],score[q][2],score[q][3]);
System.out.print(distanceBetweenParticles[p][q]);
radiusToSelect[p*(p-1)/2+q]=distanceBetweenParticles[p][q];
}
}
int first; double temp;
for (int a = radiusToSelect.length - 1; a > 0; a--)
{
first = 0; //initialize to subscript of first element
for(int b = 1; b <= a; b ++) //locate smallest element between positions 1 and i.
{
if( radiusToSelect[ b ] < radiusToSelect[ first ] )
first = b;
}
temp = radiusToSelect[first]; //swap smallest found with element in position i.
radiusToSelect[ first ] = radiusToSelect[ a ];
radiusToSelect[ a ] = temp;
}
double radius=radiusToSelect[((int) radiusToSelect.length*2/3)];
System.out.println(radius+" "+"\n");
for(int ite=0;ite<10;ite++)
{
for(int i=0;i<id.length;i++)
{
for(int j=0;j<score.length;j++)
{
distance[i][j]=distance(score[j][0],score[j][1],score[j][2],score[j][3],id[i][0],id[i][1],id[i][2],id[i][3]);
}
for(int u=0;u<distance[i].length-1;u++)
{
if(distance[i][u]>distance[i][u+1])
{
shortestdistance[i][0]=distance[i][u+1];
bmu[i][0]=u+1;
}
else
{
shortestdistance[i][0]=distance[i][u];
bmu[i][0]=u;
}
}
for(int s=0;s<score[1].length;s++)
{
for(int t=0;t<score[1].length;t++)
{
score[s][t]=score[s][t]*gaussian(score[t][0],score[t][1],score[t][2],score[t][3],score[bmu[i][0]][0],score[bmu[i][0]][1],score[bmu[i][0]][2],score[bmu[i][0]][3],radius);
System.out.print("id "+score[bmu[i][0]][t]+" ");
}
//System.out.print("\n");
}}
/*System.out.println(score[row][0]);*/
/*System.out.println(score[row][0]);*/
}
}
public static double distance (double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4)
{
return Math.sqrt((y1 - x1) * (y1 - x1) + (y2 - x2) * (y2 - x2) + (y3 - x3) * (y3 - x3) + (y4 - x4) * (y4 - x4));
}
public static double gaussian (double x1,double x2,double x3,double x4,double y1,double y2,double y3,double y4,double r)
{
return Math.exp(-(distance (x1,x2,x3, x4, y1, y2, y3, y4))/2/r/r);
}
}
答案 0 :(得分:1)
错误看起来像是在这一行:
radiusToSelect[p*(p-1)/2+q]=distanceBetweenParticles[p][q];
radiusToSelect
的长度为(score[1].length-1)*(score[1].length)/2
,即6(3 * 4/2)。 p
可以大到39,q大到38,因此它很容易超过数组的大小。