0

I'm trying to write a program to print all possible combinations of 28 variables that can be either 1 or -1 using a somewhat roundabout method, but the program isn't working. Specifically, I'm using an arraylist to store the solutions, but the stored arrays seem to be changing without my telling them to. I also tried a multidimensional array with the same result. Any help would be appreciated.

Here's my 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();
    }
5
  • 1
    what do you mean by program not working Commented Jun 23, 2014 at 22:30
  • 1
    You're storing the same array over and over again, so of course any change you make through one "view" would be visible from the others. Commented Jun 23, 2014 at 22:30
  • 3
    An array in Java is an object, and therefore vals is an object reference. Every time you say solutions.add(vals), you are adding a reference to that same array to the ArrayList; you are not making a copy of the array. Look at the Arrays.copyOf methods. Commented Jun 23, 2014 at 22:33
  • @ajb that should be an answer Commented Jun 23, 2014 at 22:35
  • Side note: don't use if(unique==true); instead use if(unique). Commented Jun 24, 2014 at 0:58

2 Answers 2

3

An array in Java is an object, and therefore vals is an object reference. Every time you say solutions.add(vals), you are adding a reference to that same array to the ArrayList; you are not making a copy of the array.

The Arrays class has some static copyOf methods to copy arrays for you. This should work, but I haven't tested it:

solutions.add(Arrays.copyOf(vals, vals.length));
Sign up to request clarification or add additional context in comments.

Comments

0

I'm trying to write a program to print all possible combinations of 28 variables that can be either 1 or -1.

final int vars = 28;

for (int i = 0; i < (1 << vars); i++) {
    int temp = i;
    for (int j = 0; j < vars; j++) {
        System.out.print((temp % 2 == 0 ? "-1" : "1"));
        System.out.print(' ');
        temp = temp / 2;
    }
    System.out.println();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.