|
-
Dec 15th, 2006, 03:05 PM
#1
Thread Starter
Member
Shifting values in array by one position - is there a better way to do it than this ?
Hi folks,
If I have a String[] set up like the following:
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());
}
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:
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
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Dec 15th, 2006, 03:57 PM
#2
Junior Member
Re: Shifting values in array by one position - is there a better way to do it than this ?
You can shorten it by combining the for loops into one for loop
-
Dec 15th, 2006, 06:34 PM
#3
Thread Starter
Member
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Dec 15th, 2006, 07:01 PM
#4
Thread Starter
Member
Re: Shifting values in array by one position - is there a better way to do it than th
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:
Code:
String[] temporaryStorageB = new String[myArray.Length-1];
This is because temporaryStorageB is only ever required to store 1 element less than that of myArray.
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Dec 15th, 2006, 07:31 PM
#5
Re: Shifting values in array by one position - is there a better way to do it than this ?
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.
-
Dec 16th, 2006, 05:50 PM
#6
Thread Starter
Member
Re: Shifting values in array by one position - is there a better way to do it than th
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.
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Dec 16th, 2006, 08:21 PM
#7
Thread Starter
Member
Re: Shifting values in array by one position - is there a better way to do it than th
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?
Code:
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.
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Dec 16th, 2006, 08:33 PM
#8
Re: Shifting values in array by one position - is there a better way to do it than this ?
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.
-
Dec 17th, 2006, 11:24 AM
#9
Thread Starter
Member
Re: Shifting values in array by one position - is there a better way to do it than th
Yes, you're correct jmcilhinney. In that case the code would be:
Code:
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);
Last edited by The Thing; Dec 17th, 2006 at 01:21 PM.
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Dec 17th, 2006, 06:00 PM
#10
Re: Shifting values in array by one position - is there a better way to do it than this ?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|