Csharp/CSharp Tutorial/Data Structure/Jagged Arrays

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

Demonstrate jagged arrays

using System; 
 
class MainClass {  
  public static void Main() {  
    int[][] jagged = new int[3][];  
    jagged[0] = new int[4];  
    jagged[1] = new int[3];  
    jagged[2] = new int[5];  
  
    int i; 
 
    Console.WriteLine("store values in first array");
    for(i=0; i < 4; i++)   
      jagged[0][i] = i;  
 
    Console.WriteLine("store values in second array");
    for(i=0; i < 3; i++)   
      jagged[1][i] = i;  
 
    Console.WriteLine("store values in third array");
    for(i=0; i < 5; i++)   
      jagged[2][i] = i;  
 
 
    Console.WriteLine("display values in first array");
    for(i=0; i < 4; i++)   
      Console.Write(jagged[0][i] + " ");  
 
    Console.WriteLine(); 
 
    Console.WriteLine("display values in second array");
    for(i=0; i < 3; i++)   
      Console.Write(jagged[1][i] + " ");  
 
    Console.WriteLine(); 
 
    Console.WriteLine("display values in third array");
    for(i=0; i < 5; i++)   
      Console.Write(jagged[2][i] + " ");  
 
    Console.WriteLine(); 
  }  
}
store values in first array
store values in second array
store values in third array
display values in first array
0 1 2 3
display values in second array
0 1 2
display values in third array
0 1 2 3 4

Demonstrate Length with jagged arrays.

using System; 
 
class MainClass {  
  public static void Main() {  
    int[][] jaggedArray = new int[4][];  
    jaggedArray[0] = new int[3];  
    jaggedArray[1] = new int[7];  
    jaggedArray[2] = new int[2];  
    jaggedArray[3] = new int[5];  
 
    int i, j; 
 
    for(i=0; i < jaggedArray.Length; i++){
      for(j=0; j < jaggedArray[i].Length; j++){
        jaggedArray[i][j] = i * j + 70;  
      }
    }
 
    Console.WriteLine("Total number of network nodes: " + 
                      jaggedArray.Length + "\n"); 
 
    for(i=0; i < jaggedArray.Length; i++) {  
      for(j=0; j < jaggedArray[i].Length; j++) { 
        Console.Write(i + " " + j + ": "); 
        Console.Write(jaggedArray[i][j] + "% ");  
        Console.WriteLine();  
      } 
      Console.WriteLine();  
    } 
  }  
}
Total number of network nodes: 4
0 0: 70%
0 1: 70%
0 2: 70%
1 0: 70%
1 1: 71%
1 2: 72%
1 3: 73%
1 4: 74%
1 5: 75%
1 6: 76%
2 0: 70%
2 1: 72%
3 0: 70%
3 1: 73%
3 2: 76%
3 3: 79%
3 4: 82%

Jagged With SubArrays

A jagged array is an array of arrays in which the length of each array can differ.


using System;
class MainClass
{
   static void Main()
   {
      int[][,] Arr;       
      Arr = new int[2][,];
      Arr[0] = new int[,] { { 1, 2 }, { 10, 20 } };
      Arr[1] = new int[,] { { 3, 4, 5 }, { 30, 40, 50 } };
      for (int i = 0; i < Arr.GetLength(0); i++)
      {
         for (int j = 0; j < Arr[i].GetLength(0); j++)
         {
            for (int k = 0; k < Arr[i].GetLength(1); k++)
            {
               Console.WriteLine ("[{0}][{1},{2}] = {3}", i, j, k, Arr[i][j, k]);
            }
         }
      }
   }
}
[0][0,0] = 1
[0][0,1] = 2
[0][1,0] = 10
[0][1,1] = 20
[1][0,0] = 3
[1][0,1] = 4
[1][0,2] = 5
[1][1,0] = 30
[1][1,1] = 40
[1][1,2] = 50

Multidimensional jagged arrays

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

Use foreach statement to loop through jagged array

using System;
class MainClass
{
   static void Main()
   {
      int[][] arr1 = new int[2][];
      arr1[0] = new int[] { 1, 3 };
      arr1[1] = new int[] { 1, 1, 4 };
      foreach (int[] array in arr1)      
      {
         Console.WriteLine("Starting new array");
         foreach (int item in array)     
         {
            Console.WriteLine(" Item: {0}", item);
         }
      }
   }
}
Starting new array
 Item: 1
 Item: 3
Starting new array
 Item: 1
 Item: 1
 Item: 4