I'm trying to create an array list of string arrays containing all possible combinations of 0s and 1s, in four dimensions. That is, [0,0,0,0] is one combination and [0,0,0,1] is another. There are $2^4$ total combinations, so I'm using several nested loops to generate this array list. However, when I try to run the loop, I get an "out of memory" error. Take a look:
String[] t4 = new String[4];
ArrayList<String[]> list4 = new ArrayList<String[]>();
for(int i=0; i<= 1; i++)
{
String count = Integer.toString(i);
t4[0]=count;
list4.add(t4);
for(int j=0; j<= 1; j++)
{
String count1 = Integer.toString(j);
t4[1]=count1;
list4.add(t4);
for(int k=0; k<= 1; k++)
{
String count2 = Integer.toString(k);
t4[2]=count2;
list4.add(t4);
for(int m=0; m<= 1;)
{
String count3 = Integer.toString(m);
t4[3]=count3;
list4.add(t4);
t4 = new String[4];
}
}
}
}
Is there something wrong with my loop? Or is there another way to generate the desired array list?
m
in yourm
for
loop. It's an infinite loop.