-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArray2.cs
More file actions
60 lines (55 loc) · 2.04 KB
/
Array2.cs
File metadata and controls
60 lines (55 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
namespace Examples {
class Program {
// The main entry point for the application.
public static void Main() {
// Creates and initializes a new three-dimensional Array of type int.
int[,,] myArr = new int[2, 3, 4];
for (int i = myArr.GetLowerBound(0); i <= myArr.GetUpperBound(0); i++) {
for (int j = myArr.GetLowerBound(1); j <= myArr.GetUpperBound(1); j++) {
for (int k = myArr.GetLowerBound(2); k <= myArr.GetUpperBound(2); k++)
myArr.SetValue((i * 100) + (j * 10) + k, i, j, k);
}
}
// Displays the properties of the Array.
Console.WriteLine("The Array has {0} dimension(s) and a total of {1} elements.", myArr.Rank, myArr.Length);
Console.WriteLine("\tLength\tLower\tUpper");
for (int i = 0; i < myArr.Rank; i++) {
Console.Write("{0}:\t{1}", i, myArr.GetLength(i));
Console.WriteLine("\t{0}\t{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i));
}
// Displays the contents of the Array.
Console.WriteLine("The Array contains the following values:");
PrintValues(myArr);
}
private static void PrintValues(int[,,] myArr) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength(myArr.Rank - 1);
while (myEnumerator.MoveNext()) {
if (i < cols)
i++;
else {
Console.WriteLine();
i = 1;
}
Console.Write("\t{0}", myEnumerator.Current);
}
Console.WriteLine();
}
}
}
// This code produces the following output:
//
// The Array has 3 dimension(s) and a total of 24 elements.
// Length Lower Upper
// 0: 2 0 1
// 1: 3 0 2
// 2: 4 0 3
// The Array contains the following values:
// 0 1 2 3
// 10 11 12 13
// 20 21 22 23
// 100 101 102 103
// 110 111 112 113
// 120 121 122 123