I have a array which contains 7 elements and i need to move 5 and 6 index elements to 1 and 2 position and vice versa
say for example
ItemParameter[] parameters = {a,b,c,d,f,g,h};
and i need
{a,g,h,d,f,b,c}
I have a array which contains 7 elements and i need to move 5 and 6 index elements to 1 and 2 position and vice versa
say for example
ItemParameter[] parameters = {a,b,c,d,f,g,h};
and i need
{a,g,h,d,f,b,c}
Using an extension method defined as:
public static void Switch<T>(this IList<T> array, int index1, int index2)
{
var aux = array[index1];
array[index1] = array[index2];
array[index2] = aux;
}
use simply as:
ItemParameter[] arr = {a,b,c,d,f,g,h};
arr.Switch(2, 6);
arr.Switch(1, 5);
ItemParameter[] arr = {a,b,c,d,f,g,h};
// swap elements of index 1 and 5
var tmp = arr[1];
arr[1] = arr[5];
arr[5] = tmp;
// swap elements of index 2 and 6
tmp = arr[2];
arr[2] = arr[6];
arr[6] = tmp;
If you need to randomize (shuffle) the array content, you can use the method below to shuffle the array content after you initialize the array.
It can be used like so:
ItemParameter[] arr = {a,b,c,d,f,g,h};
ShuffleArr(arr);
The method:
public static void ShuffleArr<T>(T[] arr)
{
Random rng = new Random();
int n = arr.Length;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = arr[k];
arr[k] = arr[n];
arr[n] = value;
}
}
Something like this ?
ItemParameter[] parameters = {a,b,c,d,f,g,h};
ItemParameter tmp = new ItemParameter();
tmp = parameters[5];
parameters[5] = parameters[0];
parameters[0] = tmp;
tmp = parameters[6];
parameters[6] = parameters[1];
parameters[1] = tmp;