반응형

배열에는 최대 32 차원 초과 있을 수 있습니다.


클래스와 달리는 System.Collections 네임 스페이스, Array 용량이 고정 합니다. 용량을 늘리려면 새 만들어야 Array 필요한 용량으로 개체, 이전에서 요소를 복사 Array 새 레코드로 개체를 삭제 하면 이전 Array합니다.

기본적으로의 최대 크기는 Array 은 2gb (기가바이트)입니다. 64 비트 환경에서 크기 제한을 설정 하 여 방지할 수 있습니다는 enabled 특성에는gcAllowVeryLargeObjects 구성 요소를 true 런타임 환경에서 합니다. 그러나 배열의 지정 된 차원 
(바이트 배열 및 단일 바이트 구조의 배열에 대 한 0X7FFFFFC7) 에 0X7FEFFFFF의 최대 인덱스는 총 4 십억 요소로 제한 수 있습니다.



배열에 둘 이상의 차원이 있을 수 있습니다. 예를 들어 다음 선언은 행 4개, 열 2개의 2차원 배열을 만듭니다.

int[,] array = new int[4, 2];

다음 선언은 세 가지 차원 4, 2, 3의 배열을 만듭니다.


int[, ,] array1 = new int[4, 2, 3];

배열 초기화

다음 예제와 같이 선언 시 배열을 초기화할 수 있습니다.


// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                        { "five", "six" } };

// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                 { { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                       { { 7, 8, 9 }, { 10, 11, 12 } } };

// Accessing array elements.
System.Console.WriteLine(array2D[0, 0]);
System.Console.WriteLine(array2D[0, 1]);
System.Console.WriteLine(array2D[1, 0]);
System.Console.WriteLine(array2D[1, 1]);
System.Console.WriteLine(array2D[3, 0]);
System.Console.WriteLine(array2Db[1, 0]);
System.Console.WriteLine(array3Da[1, 0, 1]);
System.Console.WriteLine(array3D[1, 1, 2]);

// Getting the total count of elements or the length of a given dimension.
var allLength = array3D.Length;
var total = 1;
for (int i = 0; i < array3D.Rank; i++) {
    total *= array3D.GetLength(i);
}
System.Console.WriteLine("{0} equals {1}", allLength, total);

// Output:
// 1
// 2
// 3
// 4
// 7
// three
// 8
// 12
// 12 equals 12


차수를 지정하지 않고 배열을 초기화할 수도 있습니다.


int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

초기화하지 않고 배열 변수를 선언하도록 선택할 경우 new 연산자를 사용하여 변수에 배열을 할당해야 합니다. new 사용은 다음 예제에서 확인할 수 있습니다.


int[,] array5;
array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };   // OK
//array5 = {{1,2}, {3,4}, {5,6}, {7,8}};   // Error


다음 예제에서는 특정 배열 요소에 값을 할당합니다.


array5[2, 1] = 25;

마찬가지로, 다음 예제에서는 특정 배열 요소의 값을 가져와 elementValue 변수에 할당합니다.


int elementValue = array5[2, 1];

다음 코드 예제에서는 가변 배열을 제외하고 배열 요소를 기본 값으로 초기화합니다.


int[,] array6 = new int[10, 10];







구문 소개에 앞서 배열 차원 인덱스 반환 함수 설명 (차원 숫자가 클 수록 더 안쪽에 있는 차원을 말한다)

public int GetUpperBound(
	int dimension
)

GetUpperBound(0)배열의 첫 번째 차원에서 마지막 인덱스를 반환 하 고 GetUpperBound(Rank - 1) 마지막 배열의 마지막 차원 인덱스를 반환 합니다.





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
61
62
63
using System;
using static System.Console;
 
namespace intel
{
    public class test1
    {
        public static void Main(string[] args)
        {
 
            // Create a one-dimensional integer array.
            int[] integers = { 2468101214161820 };
            // Get the upper and lower bound of the array.
            int upper = integers.GetUpperBound(0);
            int lower = integers.GetLowerBound(0);
            Console.WriteLine("Elements from index {0} to {1}:", lower, upper);
            // Iterate the array.
            for (int ctr = lower; ctr <= upper; ctr++)
                Console.Write("{0}{1}{2}", ctr == lower ? "   " : "",
                                          integers[ctr],
                                          ctr < upper ? ", " : Environment.NewLine);
 
            Console.WriteLine();
 
 
            // Create a two-dimensional integer array.
            int[,] integers2d = { 
                {24}, 
                {39}, 
                {416}, 
                {525}, 
                {636}, 
                {749}, 
                {864}, 
                {981} };
            
            // Get the number of dimensions.                               
            int rank = integers2d.Rank;
            Console.WriteLine("Number of dimensions: {0}", rank);
            for (int ctr = 0; ctr < integers2d.Rank - 1; ctr++)
            {
                Console.WriteLine("   Dimension {0}: from {1} to {2}", ctr, integers2d.GetLowerBound(ctr), integers2d.GetUpperBound(ctr));
            }
                
 
 
            // Iterate the 2-dimensional array and display its values.
            Console.WriteLine("   Values of array elements:");
            for (int outer = integers2d.GetLowerBound(0); outer <= integers2d.GetUpperBound(0); outer++)
            {
                for (int inner = integers2d.GetLowerBound(1); inner <= integers2d.GetUpperBound(1); inner++)
                {
                    Console.Write("      {3}{0}, {1}{4} = {2}", outer, inner, integers2d.GetValue(outer, inner), "{""}");
                }
                Console.WriteLine("");
            }
 
        }
 
    }
}
 
 

cs




결과

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Elements from index 0 to 9:
   2, 4, 6, 8, 10, 12, 14, 16, 18, 20
 
Number of dimensions: 2
   Dimension 0: from 0 to 7
   Values of array elements:
      {0, 0} = 2      {0, 1} = 4
      {1, 0} = 3      {1, 1} = 9
      {2, 0} = 4      {2, 1} = 16
      {3, 0} = 5      {3, 1} = 25
      {4, 0} = 6      {4, 1} = 36
      {5, 0} = 7      {5, 1} = 49
      {6, 0} = 8      {6, 1} = 64
      {7, 0} = 9      {7, 1} = 81
cs







ref : https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/arrays/multidimensional-arrays

ref : https://msdn.microsoft.com/ko-kr/library/system.array(v=vs.110).aspx

ref : https://msdn.microsoft.com/ko-kr/library/system.array.getupperbound(v=vs.110).aspx

반응형

+ Recent posts