Condividi tramite


How to: Create Multidimension Arrays

The first sample shows how to create multi-dimension arrays of reference, value, and native pointer types. It also shows how to return a multi-dimension array from a function and how to pass a multi-dimension array as an argument to a function.

The second sample shows how to perform aggregate initialization on a multi-dimension managed array.

Example

Code

// mcppv2_mdarrays.cpp
// compile with: /clr
using namespace System;

#define ARRAY_SIZE 2

ref class MyClass {
public:
   int m_i;
};

// Returns a multidimensional managed array of a reference type.
array<MyClass^, 2>^ Test0() {
   int i, j;
   array< MyClass^, 2 >^ local = gcnew array< MyClass^, 
      2 >(ARRAY_SIZE, ARRAY_SIZE);

   for (i = 0 ; i < ARRAY_SIZE ; i++)
      for (j = 0 ; j < ARRAY_SIZE ; j++) {
         local[i,j] = gcnew MyClass;
         local[i,j] -> m_i = i;
      }

      return local;
}

// Returns a managed array of Int32.
array<Int32, 2>^ Test1() {
   int i, j;
   array< Int32,2 >^ local = gcnew array< Int32,2 >(
        ARRAY_SIZE, ARRAY_SIZE);

   for (i = 0 ; i < ARRAY_SIZE ; i++)
         for (j = 0 ; j < ARRAY_SIZE ; j++)
            local[i,j] = i + 10;

   return local;
}

int main() {   
   int i, j;

   // Declares multidimensional array of user-defined reference types 
   // and initializes in a function.
   array< MyClass^, 2 >^ MyClass0;
   MyClass0 = Test0();

   for (i = 0 ; i < ARRAY_SIZE ; i++)
      for (j = 0 ; j < ARRAY_SIZE ; j++)
         Console::WriteLine("MyClass0[{0}, {1}] = {2}", i, j, 
                             MyClass0[i,j] -> m_i);

   Console::WriteLine();

   // Declare an array of value types and initializes in a function.
   array< Int32, 2 >^ IntArray;
   IntArray = Test1();

   for (i = 0 ; i < ARRAY_SIZE ; i++)
         for (j = 0 ; j < ARRAY_SIZE ; j++)
            Console::WriteLine("IntArray[{0}, {1}] = {2}", i, j, 
            IntArray[i,j]);
}

Output

MyClass0[0, 0] = 0
MyClass0[0, 1] = 0
MyClass0[1, 0] = 1
MyClass0[1, 1] = 1

IntArray[0, 0] = 10
IntArray[0, 1] = 10
IntArray[1, 0] = 11
IntArray[1, 1] = 11

Example

Description

This sample shows how to perform aggregate initialization on a multi-dimension managed array.

Code

// mcppv2_mdarrays_aggregate_initialization.cpp
// compile with: /clr
using namespace System;

ref class G {
public:
   G(int i) {}
};

value class V {
public:
   V(int i) {}
};

class N {
public:
   N(int i) {}
};

int main() {
   // Aggregate initialize a multidimension managed array.
   array<String^, 2>^ gc1 = gcnew array<String^, 2>{ {"one", "two"}, 
       {"three", "four"} };
   array<String^, 2>^ gc2 = { {"one", "two"}, {"three", "four"} };

   array<G^, 2>^ gc3 = gcnew array<G^, 2>{ {gcnew G(0), gcnew G(1)}, 
       {gcnew G(2), gcnew G(3)} };
   array<G^, 2>^ gc4 = { {gcnew G(0), gcnew G(1)}, {gcnew G(2), gcnew G(3)} };

   array<Int32, 2>^ value1 = gcnew array<Int32, 2>{ {0, 1}, {2, 3} };
   array<Int32, 2>^ value2 = { {0, 1}, {2, 3} };

   array<V, 2>^ value3 = gcnew array<V, 2>{ {V(0), V(1)}, {V(2), V(3)} };
   array<V, 2>^ value4 = { {V(0), V(1)}, {V(2), V(3)} };

   array<N*, 2>^ native1 = gcnew array<N*, 2>{ {new N(0), new N(1)}, 
      {new N(2), new N(3)} };
   array<N*, 2>^ native2 = { {new N(0), new N(1)}, {new N(2), new N(3)} };
}

See Also

Reference

array (Visual C++)