Programing

왜 우리는 들쭉날쭉 한 배열과 다차원 배열을 가지고 있습니까?

lottogame 2020. 11. 8. 09:08
반응형

왜 우리는 들쭉날쭉 한 배열과 다차원 배열을 가지고 있습니까?


  1. 들쭉날쭉 한 배열과 다차원 배열의 차이점은 무엇입니까? 서로의 이점이 있습니까?

  2. 그리고 왜 Visual Studio에서 내가

    MyClass[][] abc = new MyClass[10][20];
    

    (우리는 C ++에서이 작업을 수행했지만 C #에서는 [20]에 빨간색 물결 선으로 밑줄을 긋습니다. 잘못된 순위 지정자를 말합니다.)

    그러나 행복하다

    MyClass[,] abc = new MyClass[10,20];
    
  3. 마지막으로 어떻게 이것을 한 줄로 초기화 할 수 있습니까? (간단한 배열에서하는 것처럼 {new xxx...}{new xxx....})

    MyClass[][,][,] itemscollection;
    

  1. 들쭉날쭉 한 배열은 배열의 배열이므로 an은의 배열이며 , 각 int[][]배열 int[]은 서로 다른 길이를 가질 수 있으며 메모리에서 자체 블록을 차지할 수 있습니다. 다차원 배열 ( int[,])은 단일 메모리 블록 (본질적으로 행렬)입니다.

  2. MyClass[10][20]각 하위 배열은 별도의 개체이므로 개별적으로 초기화해야하므로을 만들 수 없습니다 .

    MyClass[][] abc = new MyClass[10][];
    
    for (int i=0; i<abc.Length; i++) {
        abc[i] = new MyClass[20];
    }
    

    A MyClass[10,20]는 괜찮습니다. 단일 객체를 10 개의 행과 20 개의 열이있는 행렬로 초기화하기 때문입니다.

  3. A MyClass[][,][,]는 다음과 같이 초기화 될 수 있습니다 (컴파일 테스트가 아님) :

    MyClass[][,][,] abc = new MyClass[10][,][,];
    
    for (int i=0; i<abc.Length; i++) {
        abc[i] = new MyClass[20,30][,];
    
        for (int j=0; j<abc[i].GetLength(0); j++) {
            for (int k=0; k<abc[i].GetLength(1); k++) {
                abc[i][j,k] = new MyClass[40,50];
            }
        }
    }
    

CLR은 1 차원 배열 액세스에 최적화되어 있으므로 들쭉날쭉 한 배열을 사용하는 것이 동일한 크기의 다차원 배열보다 빠를 가능성이 높습니다.


들쭉날쭉 한 배열은 배열의 배열입니다. 각 어레이의 크기가 같지 않을 수도 있습니다. 당신은 가질 수 있습니다

int[][] jaggedArray = new int[5][];
jaggedArray[0] = { 1, 2, 3 }; // 3 item array
jaggedArray[1] = new int[10]; // 10 item array
// etc.

관련된 배열 집합 입니다.

반면에 다차원 배열은 불규칙한 길이가없는 상자, 테이블, 큐브 등과 같이 응집력있는 그룹화에 가깝습니다. 즉 말하자면

int i = array[1,10];
int j = array[2,10]; // 10 will be available at 2 if available at 1

직사각형 배열에는 항상 모든 행에 대해 동일한 양의 열이 있습니다.

MyClass[,] x = new MyClass[10,30]

모든 행에는 30 개의 열이 있지만 들쭉날쭉 한 배열에서는 필요하지 않습니다. 따라서 들쭉날쭉 한 배열의 모든 '행'을 개별적으로 초기화해야한다고 생각합니다.

MyClass[][] x = new MyClass[10][];

for(int i = 0; i < 10; i++)
{
    x[i] = new MyClass[30];
}

사실, 이것은 들쭉날쭉 한 배열의 모든 행이 동일한 수의 요소를 포함하지 않아야 함을 의미합니다. (제 예에서는 동일한 수의 요소가 있지만 필수는 아닙니다.)

예를 들어 다음과 같이 완벽하게 수행 할 수 있습니다.

MyClass[][] x = new MyClass[10][];

for(int i = 0; i < 10; i++)
{
    x[i] = new MyClass[(30 + i)];
}

이것은 당신에게 흥미로운 기사가 ​​될 것입니다.


광고 3)와 같은 괴물을 초기화하려면 다음과 같이 [][,][,]할 수 있습니다.

        int [,][,] multiArr1 = { { new int[,] { { 2, 2 }, { 1, 1 } },
                                     new int[,] { { 2, 2 }, { 1, 1 } } },
                                     { new int[,] { { 2, 2 }, { 1, 1 } },
                                         new int[,] { { 2, 2 }, { 1, 1 } } } };
        int [,][,] multiArr2 = { { new int[,] { { 2, 2 }, { 1, 1 } },
                                     new int[,] { { 2, 2 }, { 1, 1 } } },
                                     { new int[,] { { 2, 2 }, { 1, 1 } },
                                         new int[,] { { 2, 2 }, { 1, 1 } } } };

        int [][,][,] superMultiArray = { multiArr1, multiArr2 };

경계가 설정된 다차원 배열을 찾고 있다면 항상 [,]스타일 구문을 사용하십시오 . 이렇게하면 각 부분의 크기가 동일합니다.

When you use [][] what is really going is that you're creating an array of arrays. This then means that each array can be sized differently. For example:

int[][] jaggedArray = new int[5][]
for(int index = 0; index < jaggedArray.Length ; ++index)
{
    jaggedArray[index] = new int[index + 1];
}

The inline declaration would look something like this:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };

For #1, see this SO question

For jagged or multidimensional inline arrays, see this programming guide:

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

// Same array with dimensions specified at declaration.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };

You don't have to specify the dimensions (array3D), but if you know they're never going to change, it's helpful to know what dimensions you're using (array3Da).


You would need to understand the internal working of the array the multi-dimensional array act as a single dimension array except that the double indexing is converted into a single one.

Your Jagged array in c# is an array of objects which are in turns arrays.


I think that 2d jagged arrays memory allocation in C# is like 2d arrays in C++ and C. Because 2d jagged arrays have pointer which points to array of pointers that each of this pointers points to an array of elements (for example integer elements); like this code in C++,

int** 2DArr {new int* [number1]};
for (int i = 0; i < number1; i++)
{
   2DArr[i] = new int[number2];
}

the memory allocation of code bellow is the same as 2d jagged arrays in C#. But i am doubtful about , could you please explain more if i think in wrong way.


This post is old but here are my thoughts on that.

Jagged arrays are multidimensional arrays. Multidimensional arrays come in two varieties: rectangular and jagged. Rectangular arrays represent an n-dimensional block of memory, and jagged arrays are arrays of arrays.

Rectangular arrays

Rectangular arrays are declared using commas to separate each dimension. The following statement declares a rectangular two-dimensional array, where the dimensions are 3 × 3:

int[,] matrix = new int [3, 3]; 

Jagged arrays

Jagged arrays are declared using successive square brackets to represent each dimension. Here is an example of declaring a jagged two-dimensional array, where the outermost dimension is 3:

int[][] matrix = new int[3][];

참고URL : https://stackoverflow.com/questions/4648914/why-we-have-both-jagged-array-and-multidimensional-array

반응형