0

getVariations method is used to obtain integer arrays which consist of same number of values in each but in different indexes. All these combinations are added to an arraylist called "combination" and it returns an arraylist of arrays. But when it's used as this in the main method

    int[] cp1 = { 1,5,4,3,2};
    ArrayList<int[]> k1 = sol.getVariations(cp1);
    for(int n=0; n<5 ; n++){
        int[] ab = k1.get(3);
        System.out.print(ab[n]);

    }

k1 doesn't contain the desired results. Yet couldn't find out what I'm doing wrong here. When the arraylist "combination" is checked inside this method it gives the desired results. It contains the arrays which were changed and added. But using the method's return arraylist outside the method can't get the work done.
Appreciate a lot if somebody can help.

    public ArrayList<int[]> getVariations(int[] copy){

    int[] cp1 = copy;
    int[] comb = new int[5];
    Boolean b = false;
    Boolean d = false;
    Boolean e = false;
    int m = 0;
    ArrayList<int[]> combination = new ArrayList<int[]>();
    for(int i=0; i<N ; i++){
       aloop: for(int j=0; j<N ; j++){


            for(int k=0; k<N ; k++){


                if(k==i)  {
                    b = true;
                    e = true;
                }
                if(k==i+1){
                    d = true;
                    e = false;
                }
                if(k==j){
                    b = false;
                    e = true;
                    continue;
                }
                if(b && j>=i)
                    comb[k] = cp1[k+1];
                if(!b && j>=i)
                    comb[k] = cp1[k];


                if(e && j<i){
                    comb[k] = cp1[k-1];
                }
                if((j<i || d ) && j<i && !e){
                    comb[k] = cp1[k];
                }

            }
            comb[j] = cp1[i];

            b = false;
            e = false;
            d = false;
            for(int h=0 ; h<N ; h++){
                if(comb[h] == cp1[h]){
                   b = true;
                }
                else{
                    b = false;
                    break;
                }


            }
           if(b){
               b = false;
               continue aloop;
           }

            combination.add(m , comb);
           m++;
            for(int n=0; n<N ; n++){
                int[] ab = combination.get(m-1) ;
                System.out.print(ab[n]);

            }

            System.out.println("");

        }



    }
    System.out.println("");
    return (combination);
}
3
  • 1
    Could you please reproduce your problem with a shorter and more easily understandable sample code. I'm afraid I simply don't understand your problem statement, and the code above ain't easy to revert engineer... Commented Oct 25, 2013 at 18:27
  • please provide some sample output. what you are expecting out of this is hard to find. Commented Oct 25, 2013 at 18:29
  • N = 5. it's taken from a input file Commented Oct 25, 2013 at 18:41

1 Answer 1

1

You are adding the same array comb to your ArrayList, which you have created at the beginning of your method. Adding an element happens by reference, that means all entries in your combination object are effectively the same. Try looping over the combination object just before you exit your getVariations method, you will probably notice the same behavior as outside the method.

The fix would be to create a comb object within the loop.

    public ArrayList<int[]> getVariations(int[] copy){

    int[] cp1 = copy;
    Boolean b = false;
    Boolean d = false;
    Boolean e = false;
    int m = 0;
    ArrayList<int[]> combination = new ArrayList<int[]>();
    for(int i=0; i<N ; i++){
       aloop: for(int j=0; j<N ; j++){
            int[] comb = new int[5];

            for(int k=0; k<N ; k++){
                if(k==i)  {
                    b = true;
                    e = true;
                }
                if(k==i+1){
                    d = true;
                    e = false;
                }
                if(k==j){
                    b = false;
                    e = true;
                    continue;
                }
                if(b && j>=i)
                    comb[k] = cp1[k+1];
                if(!b && j>=i)
                    comb[k] = cp1[k];


                if(e && j<i){
                    comb[k] = cp1[k-1];
                }
                if((j<i || d ) && j<i && !e){
                    comb[k] = cp1[k];
                }
            }
            comb[j] = cp1[i];

            b = false;
            e = false;
            d = false;
            for(int h=0 ; h<N ; h++){
                if(comb[h] == cp1[h]){
                   b = true;
                }
                else{
                    b = false;
                    break;
                }
            }
           if(b){
               b = false;
               continue aloop;
           }

            combination.add(m , comb);
           m++;
            for(int n=0; n<N ; n++){
                int[] ab = combination.get(m-1) ;
                System.out.print(ab[n]);

            }
            System.out.println("");
        }
    }
    System.out.println("");
    return (combination);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. After creating the comb within the loop code worked fine

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.