VBForums >
.NET >
C# > Shifting values in array by one position - is there a better way to do it than this ?
Click to See Complete Forum and Search --> : Shifting values in array by one position - is there a better way to do it than this ?
The Thing
Dec 15th, 2006, 02:05 PM
Hi folks,
If I have a String[] set up like the following:
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());
}
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?
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.
Has anyone got a better, more efficient way of doing the above? Thanks :wave:
Xizeta
Dec 15th, 2006, 02:57 PM
You can shorten it by combining the for loops into one for loop
The Thing
Dec 15th, 2006, 05:34 PM
Hi Xizeta, thanks for the info - I should have spotted that myself :(
Here's the updated code for anyone who's interested:
private 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];
myArray[i - 1] = temporaryStorageB[i-1];
}
myArray[myArray.Length - 1] = temporaryStorageA;
}
//Do something else, etc, etc.
That's a lot neater than my original attempt, thanks again Xizeta. :wave:
The Thing
Dec 15th, 2006, 06:01 PM
Another tweak to the above code is that the size of the array temporaryStorageB can be declared as being 1 less than the size of myArray.Length as in:
String[] temporaryStorageB = new String[myArray.Length-1];
This is because temporaryStorageB is only ever required to store 1 element less than that of myArray.
jmcilhinney
Dec 15th, 2006, 06:31 PM
Is it a requirement that you use an array? The whole thing would be much easier if you used a collection instead. You just Remove the first or last item, depending on which way you're rotating, then Add or Insert it. Two lines of code.
The Thing
Dec 16th, 2006, 04:50 PM
Thanks for the suggestion jmcilhinney,
the reason I hesitated in using a collection was that about a year ago on a Java programming forum it was suggested that the overhead involved was too great for what I was doing - (shunting values clockwise or anti-clockwise).
I've had a look at the documentation for C#'s Lists in the System.Collections.Generic namespace so I will have a go at that and see how I get on.
Regards.
The Thing
Dec 16th, 2006, 07:21 PM
Hi jmcilhinney,
I used a List based collection as it has additional functionality that I require
which is not available using other types of collections. Here is what I've come up with - is there any improvements you might suggest?
int numberOfElements = 10;
List<String> myList = new List<String>():
//Populate the list
for(int i = 0; i < numberOfElements; i++)
{
myList.Add(i.ToString());
}
//Rotate the values in the list anti-clockwise by 1 place
String s = aList[0].ToString();//Temporarily store a copy of the first element
aList.RemoveAt(0);//Remove from the list the first element
aList.Insert(aList.Count,s);//Insert into the end of the list the copy stored in s
//Use list elements etc, etc.
jmcilhinney
Dec 16th, 2006, 07:33 PM
You don't have to Insert to the end of a List. Just use Add, which will make the new item the last. You would only have to Insert if you were going the other way and moving the last item to the beginning.
The Thing
Dec 17th, 2006, 10:24 AM
Yes, you're correct jmcilhinney. In that case the code would be:
aList.Add(s);//Insert into the end of the list the copy stored in s
I have a habit when writing code to expressly state my intent - in the way I name methods, variables, write statements, etc - that is why I used aList.Insert(aList.Count,s); instead of aList.Add(s);
jmcilhinney
Dec 17th, 2006, 05:00 PM
Calling the Add method is expressly stating your intent. The Add method is described like so:Adds an object to the end of the List.which is exactly what you want to do.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.