Hi folks,
If I have a String[] set up like the following:
And then I want to repeatedly shift the elements of the array one position each time the method below is called, is the following code I've written the most efficient?Code:int x = 10; //The number of elements in the array String[] myArray = new String[x]; for(int i = 0; i < myArray.Length; i++) { myArray[i] = (i.ToString()); }
Has anyone got a better, more efficient way of doing the above? ThanksCode:privare void rotateArrayValues() { //This example rotates the values in the array 1 position //anti-clockwise each time it is called //Store the 1st element of the array String temporaryStorageA = myArray[0]; String[] temporaryStorageB = new String[myArray.Length]; for(int i = 1; i < myArray.Length; i++) { temporaryStorageB[i-1] = myArray[i]; } temporaryStorageB[myArray.Length-1] = temporaryStorageA; for(int i = 0; i < myArray.Length; i++) { myArray[i] = temporaryStorageB[i]; } } //Do something else, etc, etc.![]()




Reply With Quote