Csharp/CSharp Tutorial/Data Structure/Multi Dimensional Array

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A two-dimensional array

using System; 
 
class MainClass {  
  public static void Main() {  
    int t, i; 
    int[,] table = new int[3, 4];  
  
    for(t=0; t < 3; ++t) {  
      for(i=0; i < 4; ++i) {  
        table[t,i] = (t*4)+i+1;  
        Console.Write(table[t,i] + " ");  
      }  
      Console.WriteLine(); 
    }  
  } 
}
1 2 3 4
5 6 7 8
9 10 11 12

Declare, create, and initialize the rectangular array

The general form of a multidimensional array declaration:


type[, ...,] name = new type[size1, size2, ..., sizeN];

Declaring a Jagged Array

class MainClass
{
  static void Main()
  {
    int[][] cells = {
        new int[]{1, 0, 2},
        new int[]{0, 2, 0},
        new int[]{1, 2, 1}
    };
    cells[1][0] = 1;
  }
}

Declaring a Two-Dimensional Array

class MainClass
{
  static void Main()
  {
    int[,] cells = new int[3,3];
  }
}

Initialize a two-dimensional array

using System; 
 
class MainClass {  
  public static void Main() {  
    int[,] sqrs = { 
      { 1, 1 }, 
      { 2, 4 }, 
      { 3, 9 }, 
      { 4, 16 }, 
      { 5, 25 }, 
      { 6, 36 }, 
      { 7, 49 }, 
      { 8, 64 }, 
      { 9, 81 }, 
      { 10, 100 } 
    }; 
    int i, j; 
 
    for(i=0; i < 10; i++) {  
      for(j=0; j < 2; j++)  
        Console.Write(sqrs[i,j] + " ");   
      Console.WriteLine();  
    }  
  }  
}
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

Initialize multidimensional arrays in declaration

using System;
class MainClass
{
    public static void Main()
    {
    int[,] matrix = { {1, 1}, {2, 2}, {3, 5}, {4, 5}, {134, 44} };
        
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                Console.WriteLine("matrix[{0}, {1}] = {2}", i, j, matrix[i, j]);
            }
        }    
    }
}
matrix[0, 0] = 1
matrix[0, 1] = 1
matrix[1, 0] = 2
matrix[1, 1] = 2
matrix[2, 0] = 3
matrix[2, 1] = 5
matrix[3, 0] = 4
matrix[3, 1] = 5
matrix[4, 0] = 134
matrix[4, 1] = 44

Initializing a Jagged Array

class MainClass
{
  static void Main()
  {
    int[][] cells = {
        new int[]{1, 0, 2, 0},
        new int[]{1, 2, 0},
        new int[]{1, 2},
        new int[]{1}
    };
  }
}

Initializing a Two-Dimensional Array of Integers

class MainClass
{
  static void Main()
  {
    int[,] cells = {
            {1, 0, 2},
            {0, 2, 0},
            {1, 2, 1}
        };
    // Set the winning tic-tac-toe move to be player 1.
    cells[1,0] = 1;
  }
}

Retrieving a Particular Dimension"s Size

class MainClass
{
  static void Main()
  {
    bool[,,] cells;
    cells = new bool[2,3,3];
    System.Console.WriteLine(cells.GetLength(0));   // Displays 2
  }
}

Sum the values on a diagonal of a atrix

using System; 
 
class MainClass {  
  public static void Main() {  
    int[,,] m = new int[3, 3, 3]; 
    int sum = 0; 
    int n = 1; 
 
    for(int x=0; x < 3; x++) 
      for(int y=0; y < 3; y++) 
        for(int z=0; z < 3; z++)  
          m[x, y, z] = n++; 
 
 
    sum = m[0,0,0] + m[1,1,1] + m[2, 2, 2]; 
 
    Console.WriteLine("Sum of first diagonal: " + sum); 
  }  
}
Sum of first diagonal: 42

The use of a three-dimensional rectangular array

using System;
class MainClass
{
  public static void Main()
  {
    int[,,] intArray = new int [10, 5, 3];
    intArray[1, 3, 2] = 3;
    intArray[4, 1, 2] = 9;
    Console.WriteLine("intArray.Rank (number of dimensions) = " + intArray.Rank);
    Console.WriteLine("intArray.Length (number of elements) = " + intArray.Length);
    for (int x = 0; x < intArray.GetLength(0); x++)
    {
      for (int y = 0; y < intArray.GetLength(1); y++)
      {
        for (int z = 0; z < intArray.GetLength(2); z++)
        {
          if (intArray[x, y, z] != 0)
          {
            Console.WriteLine("intArray[" + x + ", " + y + ", " + z +"] = " + intArray[x, y, z]);
          }
        }
      }
    }
  }
}
intArray.Rank (number of dimensions) = 3
intArray.Length (number of elements) = 150
intArray[1, 3, 2] = 3
intArray[4, 1, 2] = 9

Use a single for loop to assign a two-dimensional array

using System;
class MainClass
{
  static void Main(string[] args)
  {
    //multidimensional array
    int[,] MyIntArray = new int[5, 5];
    for (int x = 0, y = 0; x < 5; x++, y++)
    {
      MyIntArray[x, y] = 0;
    }
  }
}

Use foreach on a two-dimensional array.

using System; 
 
class MainClass { 
  public static void Main() { 
    int sum = 0; 
    int[,] nums = new int[3,5]; 
 
    for(int i = 0; i < 3; i++)  
      for(int j=0; j < 5; j++) 
        nums[i,j] = (i+1)*(j+1); 
 
    foreach(int x in nums) { 
      Console.WriteLine("Value is: " + x); 
      sum += x; 
    } 
    Console.WriteLine("Summation: " + sum); 
  } 
}
Value is: 1
Value is: 2
Value is: 3
Value is: 4
Value is: 5
Value is: 2
Value is: 4
Value is: 6
Value is: 8
Value is: 10
Value is: 3
Value is: 6
Value is: 9
Value is: 12
Value is: 15
Summation: 90

Use Foreach statement to loop through Rectangular Array

using System;
class MainClass
{
   static void Main()
   {
      int[,] arr1 = { { 0, 1 }, { 2, 3 } };
      foreach (int element in arr1)
      {
         Console.WriteLine ("Element: {0}", element);
      }
   }
}
Element: 0
Element: 1
Element: 2
Element: 3