dimanche 22 février 2015

Unable to increase the size of the array dynamically in java



I'm trying to increase the size of a two dimensional array dynamically .However,I'm getting an "Array out of Bounds"error it.Below posted is my code.In this scenario,I'm trying to add values dynamically to the two dimmensional vector "weight[][]" under some condition.Any Suggestions would be highly helpful!



class perceptron
{
public static void main(String[] args)
{


int[][] myPoints = {{2,3},{4,5},{7,8},{9,10}};
int [] classlabels = {1,1,-1,-1};
int [] [] weight = {{0,0}};
int base=0;

int i,j;
int activation=0;
for(i=0;i<myPoints.length;i++)
{
for(j=0;j<classlabels.length;j++)
{

activation = activation+((myPoints[i][0]*weight[i][0])+(myPoints[i]
[1]*weight[i][1]));

System.out.println("dot product of two vectors" + activation);
if((activation*classlabels[j]) <= 0)
{
System.out.println("hi");

weight[i+1][0] = weight[i][0]+ (classlabels[j]*myPoints[i]
[0]);
weight [i+1][1] =weight[i][1]+(classlabels[j]*myPoints[i]
[1]);

System.out.println(weight[i+1][0]);
System.out.println(weight[i+1][1]);

base = base+classlabels[j];
System.out.println(base);
}

}

}
}
}



Aucun commentaire:

Enregistrer un commentaire