Mar 18
It’s easy to reverse the contents of an array using C# generics:
static T[] ReverseArray<T>( T[] array ) { T[] newArray = null; int count = array == null ? 0 : array.Length; if (count > 0) { newArray = new T[count]; for (int i = 0, j = count - 1; i < count; i++, j--) { newArray[i] = array[j]; } } return newArray; }
And here is an example of how you might call this method:
int[] intArray = new int[] { 1, 2, 3, 4 }; int[] intArrayReversed = ReverseArray<int>( intArray ); string[] stringArray = new string[] { "A", "B", "C", "D" }; string[] stringArrayReversed = ReverseArray<string>( stringArray );
Note that this method should be used only when you want to create a NEW array. If you simply want to reverse an existing array in place, use the static Array.Reverse method.
Here is a console program to test this method:
using System; namespace ReverseArray { class Program { static void Main( string[] args ) { TestReverse<int>( new int[0] ); TestReverse<int>( new int[] { 1 } ); TestReverse<int>( new int[] { 1, 2 } ); TestReverse<int>( new int[] { 1, 2, 3 } ); TestReverse<int>( new int[] { 1, 2, 3, 4 } ); TestReverse<string>( null ); TestReverse<string>( new string[] { "A" } ); TestReverse<string>( new string[] { "A", "B" } ); TestReverse<string>( new string[] { "A", "B", "C" } ); TestReverse<string>( new string[] { "A", "B", "C", "D" } ); Console.ReadLine(); } static void TestReverse<T>( T[] array ) { Write( array ); array = ReverseArray<T>( array ); Write( array ); Console.WriteLine(); } static void Write<T>( T[] array ) { if (array != null) { foreach (T i in array) { Console.Write( "{0} ", i ); } } Console.WriteLine(); } static T[] ReverseArray<T>( T[] array ) { T[] newArray = null; int count = array == null ? 0 : array.Length; if (count > 0) { newArray = new T[count]; for (int i = 0, j = count - 1; i < count; i++, j--) { newArray[i] = array[j]; } } return newArray; } } }
Here is the program output:
1
11 2
2 11 2 3
3 2 11 2 3 4
4 3 2 1A
AA B
B AA B C
C B AA B C D
D C B A
why no just use the tool .NET gave us like:
int[] arrInt = new int[] { 1, 2, 3 };
Array.Reverse(arrInt);
it’s a bit shorter and easier i think ..
Hi Idan, you are correct. If you want to reverse the array in place, you should use Array.Reverse(). The generic method in this post is to reverse the array into a new array. I updated my post to make that more clear. Thank you for your comment.
If you wish to reverse the contents of a string, there is a very simple method using LINQ:
string ReverseSentence(string sentence)
{
string[] words = sentence.Split(‘ ‘);
return words.Aggregate((a, b) => b + ” ” + a);
}
Of course, this would only work on strings 🙂
http://blogs.msdn.com/charlie/archive/2008/02/06/linq-farm-seed-02-aggregate-operator-part-ii.aspx
Referring to Idan why is it that whenever you go to an interview you get questions like this. Is it like to test your logic?
I remember I was asked to to convert a string to an integer atoi and of course I started using the int32.tryparse method and the interviewers immediately said “you don’t get to do that”
is there a site with these kinda problems where we can go and study?
Hi Hades, Google “.NET interview questions” to find many sites with questions and study guides .NET programming interviews.
Why would anybody want to allocate extra object to reverse an array? Despite of the fact, that Array.Reverse already exists I don’t see the point in implementing it that way.
And even than why not use Array.Copy for the copy operation?
It would also be nice to use Unit Tests for the test code.
it’s not working