Lunayah - 8 months ago 92

Java Question

I have been performing array related test where I try to swap array following a specific pattern and then I try to swap the array back to how it was originally using the pattern in reverse it always worked. As I was performing my tests I ran into a tricky situation where my reversal pattern simply wasn't doing the job! I have decided to post my problem here to see if anyone has any insight into what it is I am missing when I try to reverse my array! is the array not reversible?

The array along with the swapping pattern:

`String[] strs = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};`

for(int i = 0; i<strs.length-2; i++){

String temp = strs[i];

strs[i] = strs[i+2];

strs[i+2] = temp;

for(int r = 0; r<strs.length-6; r++){

String temp2 = strs[r];

strs[r] = strs[r+6];

strs[r+6] = temp2;

for(int s = 0; s<strs.length-4; s++){

String temp3 = strs[s];

strs[s] = strs[s+4];

strs[s+4] = temp3;

for(int t = 0; t<strs.length-2; t++){

String temp4 = strs[t];

strs[t] = strs[t+2];

strs[t+2] = temp4;

}

}

}

}

Result: G B E H I N M T O D A L S V Q P W F Y X C J K R U Z

I have originally try to revert back the array using the same formula in reverse

but even when it would compile without index out of bounds exceptions it will still not revert the array back to its original state!

`for(int i = 0; i<strs.length; i++)`

System.out.print(strs[i]+" ");

System.out.println("");

for(int i = strs.length-3; i>=0; i--){

String temp = strs[i];

strs[i] = strs[i+2];

strs[i+2] = temp;

for(int r = strs.length-7; r>=0; r--){

String temp2 = strs[r];

strs[r] = strs[r+6];

strs[r+6] = temp2;

for(int s = strs.length-5; s>=0; s--){

String temp3 = strs[s];

strs[s] = strs[s+4];

strs[s+4] = temp3;

for(int t = strs.length-3; t>=0; t--){

String temp4 = strs[t];

strs[t] = strs[t+2];

strs[t+2] = temp4;

}

}

}

}

for(int i = 0; i<strs.length; i++)

System.out.print(strs[i]+" ");

Result: G N O P W R E B C H A D I T Q Z K L M V S F Y J U X

Here is the Reversed version as suggested by

`for(int i = 0; i<strs.length-2; i++){`

String temp = strs[i];

strs[i] = strs[i+2];

strs[i+2] = temp;

for(int r = 0; r<strs.length-6; r++){

String temp2 = strs[r];

strs[r] = strs[r+6];

strs[r+6] = temp2;

for(int s = 0; s<strs.length-4; s++){

String temp3 = strs[s];

strs[s] = strs[s+4];

strs[s+4] = temp3;

for(int t = 0; t<strs.length-2; t++){

String temp4 = strs[t];

strs[t] = strs[t+2];

strs[t+2] = temp4;

}

}

}

}

Result: I X Y B E N K T G P S D Q Z O F A H M V U L C J W R

Answer

In order to reverse your previous algorithm, you also need to reverse the order of the blocks inside each loop (first main swap, then inner loop) to (first inner loop then swap), like this:

```
for(int i = strs.length-3; i>=0; i--) {
for(int r = strs.length-7; r>=0; r--) {
for(int s = strs.length-5; s>=0; s--) {
for(int t = strs.length-1; t>=3; t--) {
String temp4 = strs[t];
strs[t] = strs[t-3];
strs[t-3] = temp4;
}
String temp3 = strs[s];
strs[s] = strs[s+4];
strs[s+4] = temp3;
}
String temp2 = strs[r];
strs[r] = strs[r+6];
strs[r+6] = temp2;
}
String temp = strs[i];
strs[i] = strs[i+2];
strs[i+2] = temp;
}
```

This will actually put your array back in its original state. Try it out.