Java Array List is overriding the data added and put the last items added only.
The output expected is 1.0, 1.0, -1.0,-1.0, -1.0, 1.0,-1.0, -1.0, -1.0,-1.0, -1.0, -1.0
But, the output I get is -1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0 .
The code override the list by last array elements which is a=[-1,-1,-1,-1]. That is why all 12 elements of the list value is overridden to -1.
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args){
double sum=0; int n=0,y=0,count=0,x=1;
double []a=new double []{1,0,1};
double []b=new double [a.length];
List<double[] >mylist= new ArrayList<>();
double [][]c=new double[][]{{0,0.66,0.66},{0.66,0,0.66},{0,0.66,0}};
mylist.add(0,a);
while(n>=0 && n<4){
for (int i = 0 ; i < 3 ; i++)
for (int j = 0 ; j < a.length; j++)
{
sum=sum+(c[i][j]*a[j]);
if(j==a.length-1)
{
if(sum>0)
sum=1;
else
sum=-1;
b[y]=sum;
y++;
count++;
sum=0;
}
if(count==a.length){
mylist.add(x,b);
x++;
y=0;
count=0;
for(int k=0;k<a.length;k++){
a[k]=b[k];
}
}
}n++;
}
for (int i = 0 ; i < mylist.size(); i++)
for (int j = 0 ; j < a.length ; j++)
{
System.out.print(mylist.get(i)[j]+",");
}
}
}