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);
}