Two-Dimensional Arrays |
|
The arrays we used so far were made of a uniform series, where all members consisted of a simple list, like a column of names on a piece of paper. Also, all items fit in one list. In some cases, you may want to divide the list in delimited sections. For example, if you create a list of names, you may want part of the list to include family members and another part of the list to include friends. Instead of creating a second list, you can add a second dimension to the list. In other words, you would like to create a list of a list, or one list inside of another list, although the list is still made of items with common characteristics. A multi-dimensional array is a series of lists so that each list contains its own list. For example, if you create two lists of names, you would have an array of two lists. Each array or list would have its own list or array. |
The most basic multi-dimensional array is an array of an array, also referred to as two-dimensional. To create a two-dimensional array, declare the array variable as we did earlier but add a comma in the square brackets. The formula you would use is: DataType[,] VariableName; The pair of brackets is empty but must contain a comma. There are two ways you can use a two-dimensional array. If you are declaring the array variable but are not ready to initialize it, use the following formula: DataType[,] VariableName = new DataType[Number1,Number2]; Based on this, type two integers separated by a comma in the right square brackets. Here is an example: using System; namespace CSharpLessons { class Exercise { static void Main() { string[,] Members = new string[2,4]; } } In this declaration, the Members variable contains two lists. Each of the two lists contains 4 items. This means that the first list contains 4 items, the second list contains 4 items also. Therefore, the whole list is made of 8 items (2*4=8). Because the variable is declared as a string, each of the 8 items is a string. You can initialize an array variable when declaring it. To do this, on the right side of the declaration, before the closing semi-colon, type an opening and a closing curly brackets. Inside of the brackets, include a pair of an opening and a closing curly brackets for each internal list of the array. Then, inside of a pair of curly brackets, provide a list of the values of the internal array, just as you would do for a one-dimensional array. Here is an example: using System; namespace CSharpLessons { class Exercise { static void Main() { String[,] members = new String[2,4]{ {"Celeste", "Mathurin", "Alex", "Germain"}, {"Jeremy", "Mathew", "Anselme", "Frederique"} }; Console.WriteLine(); } } If you use this technique to initialize an array, you can omit specifying the dimension of the array.
To use the members of a two-dimensional array, you can access each item individually. For example, to initialize a two-dimensional array, you can access each member of the array and assign it a value. The external list is zero-based. In other words, the first list has an index of 0, the second list has an index of 1. Internally, each list is zero-based and behaves exactly like a one-dimensional array. To access a member of the list, type the name of the variable followed by its square brackets. In the brackets, type the index of the list, a comma, and the internal index of the member whose access you need. Here is an example: using System; namespace CSharpLessons { class Exercise { static void Main() { String[,] members = new String[2,4]; members[0,0] = "Celeste"; members[0,1] = "Mathurin"; members[0,2] = "Alex"; members[0,3] = "Germain"; members[1,0] = "Jeremy"; members[1,1] = "Mathew"; members[1,2] = "Anselme"; members[1,3] = "Frederique"; Console.WriteLine(); } }
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|