
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
288
|
Chapter 5: Collections
Solution
Use the ReverseJaggedArray<T> method:
public static void ReverseJaggedArray<T>(T[][] theArray)
{
for (int rowIndex = 0; rowIndex <= (theArray.GetUpperBound(0)); rowIndex++)
{
for (int colIndex = 0;
colIndex <= (theArray[rowIndex].GetUpperBound(0) / 2);
colIndex++)
{
T tempHolder = theArray[rowIndex][colIndex];
theArray[rowIndex][colIndex] =
theArray[rowIndex][theArray[rowIndex].GetUpperBound(0) -
colIndex];
theArray[rowIndex][theArray[rowIndex].GetUpperBound(0) - colIndex] =
tempHolder;
}
}
}
Discussion
The following TestReverseJaggedArray method shows how the ReverseJaggedArray<T>
method is used:
public static void TestReverseJaggedArray( )
{
int[][] someArray =
new int[][] {new int[3] {1,2,3}, new int[6]{10,11,12,13,14,15}};
// Display the original array.
for (int rowIndex = 0; rowIndex < someArray.Length; rowIndex++)
{
for (int colIndex = 0; colIndex < someArray[rowIndex].Length; colIndex++)
{
Console.WriteLine(someArray[rowIndex][colIndex]);
}
}
Console.WriteLine( );
ReverseJaggedArray(someArray);
// Display the reversed array.
for (int rowIndex = 0; rowIndex < someArray.Length; rowIndex++)
{
for (int colIndex = 0; colIndex < someArray[rowIndex].Length; colIndex++)
{
Console.WriteLine(someArray[rowIndex][colIndex]);
}
}
}