
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
282
|
Chapter 5: Collections
int[,,] bar = new int[2,3,4]; // A 3-dimensional array
// containing 24 elements
A two-dimensional array is usually described as a table with rows and columns. The
foo array would be described as a table of two rows, each containing three columns
of elements. A three-dimensional array can be described as a cube with layers of
tables. The
bar array could be described as four layers of two rows, each containing
three columns of elements.
Jagged arrays are arrays of arrays. If you picture a jagged array as a one-dimensional
array with each element in that array containing another one-dimensional array, it
could have a different number of elements in each row. A jagged array is defined as
follows:
int[][] baz = new int[2][] {new int[2], new int[3]};
The baz array consists of a one-dimensional array containing two elements. Each of
these elements consists of another array, the first array having two elements and the
second array having three.
The rest of this chapter contains recipes dealing with arrays and the various collec-
tion types.
5.1 Swapping Two Elements in an Array
Problem
You want an efficient method to swap two elements that exist within a single array.
Solution
Use the generic SwapElementsInArray<T> method:
public static void SwapElementsInArray<T>(T[] theArray, ...