java - Issue with multidimensional arrays/ arraylist of arrays -
java - Issue with multidimensional arrays/ arraylist of arrays -
i'm trying write programme print possible combinations of 28 variables can either 1 or -1 using roundabout method, programme isn't working. specifically, i'm using arraylist store solutions, stored arrays seem changing without telling them to. tried multidimensional array same result. help appreciated.
here's code:
int[] vals = new int [28]; arrays.fill(vals, 1); arraylist <int[]> solutions = new arraylist<int[]> (756); long c=0; //counter int ns=0; //number of solutions found while (ns<756){ c++; for(int i=0;i<28;i++){ if (c%(i+1)==0){ vals[i]*=-1; } } boolean unique = true; for(int i=0;i<ns;i++){ if(arrays.equals(vals, solutions.get(i)) ){ unique = false; } } if(unique==true){ solutions.add(vals); ns++; } } for(int i=0;i<756;i++){ system.out.println( "solution "+ i); for(int j=0;j<28;j++){ system.out.println("1: " + solutions.get(i)[j]); } system.out.println(); }
an array in java object, , hence vals
object reference. every time solutions.add(vals)
, adding reference same array arraylist
; not making re-create of array.
the arrays
class has static copyof
methods re-create arrays you. should work, haven't tested it:
solutions.add(arrays.copyof(vals, vals.length));
java arrays arraylist
Comments
Post a Comment