Results 1 to 10 of 10

Thread: Shifting values in array by one position - is there a better way to do it than this ?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile 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 ~

  2. #2
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile Re: Shifting values in array by one position - is there a better way to do it than th

    Hi Xizeta, thanks for the info - I should have spotted that myself

    Here's the updated code for anyone who's interested:

    Code:
    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.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile 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 ~

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile 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 ~

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile 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 ~

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile 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 ~

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width