Is there any way to move items inside an array? For example:
int[] myArray = {1,2,3,4};
2nd element becomes the last:
int[] myArray = {1,3,4,2};
P.S.: No that’s not a homework. I can think of at least one solution but it requires rather difficult implementation:
- First we save second element to Int
- then we remove this element from the array
- then we add new element at the very end of my array
Any other (read – easier) way to do this?
There is no easy way to do it using an array. You’ll have to loop through the array shifting every element up to the index that’s moving, and then re-insert that element at the end. You could always use a
List<int>to do it.