< C++ .NET Arrays & Collections 4 | Main | C++ .NET Arrays & Collections 6 >


 

 

Arrays and Collections 5

 

 

What we have in this page?

 

  1. Multidimensional Managed Arrays

  2. Creating Arrays of Managed Arrays

 

 

Multidimensional Managed Arrays

 

The multi dimensional array uses the same syntax as one dimensional array by setting the optional dimension parameter as shown below.

[qualifiers] [cli::]array<[qualifiers]type1[, dimension]>^ var = gcnew [cli::]array<type2[, dimension]>(val[,val...])

Use the previous project and try the following example.

// RefArray.cpp : main project file.

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

#define ARRAY_SIZE 3

 

ref class MyClass

{

   public:

      int m_i;

      // Constructor

      MyClass()

      {

         Console::WriteLine("In MyClass...");

      }

};

 

// 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 + 10;

     }

   return local;

}

 

int main(array<System::String ^> ^args)

{

   int i, j;

   // Declares multidimensional array of user-defined reference types

   // and initializes in a function.

   Console::WriteLine("Declares multidimensional array of user-defined\n"

         "reference types and initializes in a function");

   // 2-dimension array...

   array< MyClass^, 2 >^ MyClass0;

   MyClass0 = Test0();

   Console::WriteLine();

   Console::WriteLine("2D array: index & content");

   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);

   return 0;

}

 

Output:

 

The multi dimensional array console console program output sample

 

The following example 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 example that follows shows how to perform aggregate initialization on a multi-dimension managed array.

// RefArray.cpp : main project file.

// compile with: /clr

#include "stdafx.h"

 

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(array<System::String ^> ^args)

{

      int i, j;

      // Declares multidimensional array of user-defined reference types

      // and initializes in a function.

      Console::WriteLine("Declares multidimensional array of user-defined\n"

            "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.

      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]);

      return 0;

}

 

Output:

A console program output example that shows how to create multi-dimension arrays of reference, value, and native pointer types

 

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

// RefArray.cpp : main project file.

// compile with: /clr

#include "stdafx.h"

 

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(array<System::String ^> ^args)

{

   // Aggregate initialize a multi dimension 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)}};

 

   return 0;

}

// No output

Creating Arrays of Managed Arrays

 

These samples show how to create single-dimension arrays of managed array of reference, value, and native pointer types. It also shows how to return a single-dimension array of managed array from a function and how to pass a single-dimension array as an argument to a function.

// RefArray.cpp : main project file.

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

#define ARRAY_SIZE 2

 

value struct MyStruct

{

   int strucin;

};

 

ref class MyClass

{

   public:

     int clasin;

};

 

// Returns an array of managed arrays of a reference type.

array<array<MyClass^>^>^ Test0()

{

   int size_of_array = 4;

   array<array<MyClass^>^>^ local = gcnew array<array<MyClass^>^>(ARRAY_SIZE);

 

   for(int i = 0 ; i < ARRAY_SIZE; i++, size_of_array += 4)

   {

      local[i] = gcnew array<MyClass^>(size_of_array);

      for (int k = 0; k < size_of_array ; k++)

      {

          local[i][k] = gcnew MyClass;

          local[i][k]->clasin = i;

      }

   }

   return local;

}

 

// Returns a managed array of Int32.

array<array<Int32>^>^ Test1()

{

   int i;

   array<array<Int32>^>^ local = gcnew array<array< Int32 >^>(ARRAY_SIZE);

 

   for (i = 0; i < ARRAY_SIZE; i++)

   {

      local[i] = gcnew array< Int32 >(ARRAY_SIZE);

      for (int j = 0; j < ARRAY_SIZE; j++)

         local[i][j] = i + 10;

   }

   return local;

}

 

int main(array<System::String ^> ^args)

{

      int i, j;

      // Declares an array of user-defined reference types

      // and initializes with a function.

      Console::WriteLine("Declares an array of user-defined reference types\n"

            "and initializes with a function");

      array< array< MyClass^ >^ >^ MyClass0;

      MyClass0 = Test0();

 

      for (i = 0; i < ARRAY_SIZE; i++)

         for (j = 0; j < ARRAY_SIZE; j++)

            Console::WriteLine("MyClass0[{0}] = {1}", i, MyClass0[i][j]->clasin);

      Console::WriteLine();

      // Declares an array of value types and initializes with a function.

      Console::WriteLine("Declares an array of value types and initializes with a function");

      array< array< Int32 >^ >^ IntArray;

      IntArray = Test1();

 

      for (i = 0; i < ARRAY_SIZE; i++)

         for (j = 0; j < ARRAY_SIZE; j++)

            Console::WriteLine("IntArray[{0}] = {1}", i, IntArray[i][j]);

      Console::WriteLine();

      // Declares and initializes an array of user-defined value types.

      Console::WriteLine("Declares and initializes an array of user-defined value types");

      array< MyStruct >^ MyStruct1 = gcnew array< MyStruct >(ARRAY_SIZE);

      for (i = 0; i < ARRAY_SIZE; i++)

      {

        MyStruct1[i] = MyStruct();

        MyStruct1[i].strucin = i + 40;

      }

      for (i = 0; i < ARRAY_SIZE; i++)

         Console::WriteLine("MyStruct1[{0}].strucin: {1}", i, MyStruct1[i].strucin);

 

      return 0;

}

 

Output:

A console program output that shows how to create single-dimension arrays of managed array of reference, value, and native pointer types  

 

The following sample shows how to perform aggregate initialization with jagged arrays.

// RefArray.cpp : main project file.

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

#define ARRAY_SIZE 2

 

int size_of_array = 4;

int count = 0;

 

ref class MyClass

{

  public:

     int m_i;

};

 

struct MyNativeClass

{

   int m_i;

};

 

int main(array<System::String ^> ^args)

{

  // Declares an array of user-defined reference types

  // and performs an aggregate initialization

  array< array< MyClass^ >^ >^ MyClass0 = gcnew array<array<MyClass^>^>

  {

     gcnew array<MyClass^>{gcnew MyClass(), gcnew MyClass()},

            gcnew array<MyClass^>{gcnew MyClass(), gcnew MyClass()}

  };

 

  for (int i = 0; i < ARRAY_SIZE; i++, size_of_array += 4)

     for (int k = 0; k < ARRAY_SIZE; k++)

        MyClass0[i][k]->m_i = i;

        for (int i = 0; i < ARRAY_SIZE; i++)

          for (int j = 0; j < ARRAY_SIZE; j++)

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

          Console::WriteLine();

 

      // Declares an array of value types and performs an aggregate initialization

      array< array< Int32 >^ >^ IntArray = gcnew array<array< Int32 >^>

      {

        gcnew array<Int32>{1,2},

        gcnew array<Int32>{3,4,5}

      };

 

      for each (array<int>^ outer in IntArray)

      {

         Console::Write("[");

         for each(int i in outer)

            Console::Write(" {0}", i);

         Console::Write(" ]");

         Console::WriteLine();

      }

      Console::WriteLine();

 

      // Declares and initializes an array of pointers to a native type.

      array<array< MyNativeClass * >^ > ^ MyClass2 = gcnew array<array< MyNativeClass * > ^>

      {

        gcnew array<MyNativeClass *>{new MyNativeClass(), new MyNativeClass()},

         gcnew array<MyNativeClass *>{new MyNativeClass(), new MyNativeClass(), new MyNativeClass()}

      };

 

      for each (array<MyNativeClass *> ^ outer in MyClass2)

        for each(MyNativeClass* i in outer)

           i->m_i = count++;

 

      for each (array<MyNativeClass *> ^ outer in MyClass2)

      {

         Console::Write("[");

         for each(MyNativeClass* i in outer)

           Console::Write(" {0}", i->m_i);

         Console::Write(" ]");

         Console::WriteLine();

      }

      Console::WriteLine();

      // Declare and initialize array of two-dimensional arrays of strings

      array<array<String ^,2> ^> ^gc3 = gcnew array<array<String ^,2> ^>

      {

        gcnew array<String ^>{{"a","b"}, {"c", "d"}, {"e","f"}},

           gcnew array<String ^>{{"g", "h"}}

      };

 

      for each (array<String^, 2> ^ outer in gc3)

      {

         Console::Write("[");

         for each(String ^ i in outer)

           Console::Write(" {0}", i);

         Console::Write(" ]");

         Console::WriteLine();

   }

      return 0;

}

 

Output:

A console program output example that shows how to perform aggregate initialization with jagged arrays

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8

 


 

< C++ .NET Arrays & Collections 4 | Main | C++ .NET Arrays & Collections 6 >