0

I have a question regarding a 1d array and what I am attempting to do with this array is changing the index value of any value within the array for example

int[] num = new[2,4,6,9]

and what I want to do with this array I want position 0 to become position 1 and position 1 to become 0. So the array would look like [4,2,6,9] and that part is easy enough to do but I am struggling with the parts that come after which is I would like for the array to continue down this path so [4,6,2,9]->[4,6,9,2] and I am struggling with that. So far I am using two array to try this but I am having difficulties. Also I am attempting to do this with all spots and not just the first one.

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        temp[j] = temp2[i];
        if (j != 0) {
            temp[j - 1] = temp2[j];
        }
    }
    revert(); //I use this methods to restore any changes made so I can attempt with the next spot
} 
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Dave
  • 13
  • 4

1 Answers1

0

Keep only 1 for loop and 1 temp variable to swap your tab.

for (int i = 0; i < N; i++) {
  tmp = tab[i];
  tab[i] = tab[i + 1];
  tab[i + 1] = tmp;
}
gfkl
  • 99
  • 1
  • 4