C# 二维数组(2d Array)解析
在这篇文章中,我将会用示例讨论二维数组。做为文章的部分内容,我们会讨论:
1、什么是二维数组?
2、以示例解析矩形数组和交错数组。
什么是二维数组?
在C#中,以行和列的形式存储元素的数组称为二维数组。C#中,二维数组也叫多维数组,有两种类型。
1、矩形数组:行和列相等的数组叫做矩形数组。
2、行和列不相等的数组称为交错数组。
C#中的矩形数组
让我们了解一下二维数组的语法,请看一下下面的图表
让我们用示例来更好地理解一下矩形数组
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace _2DarrayDemo 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 int[,] arr = new int[4,5]; 15 int a = 0; 16 17 foreach (int i in arr) 18 { 19 Console.Write(i + " "); 20 } 21 Console.WriteLine("\n"); 22 23 for (int i = 0; i < arr.GetLength(0); i++) //0指的是一维 24 { 25 for (int j = 0; j < arr.GetLength(1); j++)//1指的是二维 26 { 27 a += 5; 28 arr[i, j] = a; 29 Console.WriteLine("i is {0};j is {1}",i,j); 30 } 31 } 32 for (int i = 0; i < arr.GetLength(0); i++) 33 { 34 for (int j = 0; j < arr.GetLength(1); j++) 35 { 36 Console.Write(arr[i,j] + " "); 37 } 38 } 39 Console.ReadKey(); 40 } 41 } 42 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace _2DarrayDemo 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 int[,] arr = new int[4,5]; 15 int a = 0; 16 17 foreach (int i in arr) 18 { 19 Console.Write(i + " "); 20 } 21 Console.WriteLine("\n"); 22 23 for (int i = 0; i < arr.GetLength(0); i++) //0指的是一维 24 { 25 for (int j = 0; j < arr.GetLength(2); j++)//如果将1修改为2,则会报错; 26 { 27 a += 5; 28 arr[i, j] = a; 29 Console.WriteLine("i is {0};j is {1}",i,j); 30 } 31 } 32 for (int i = 0; i < arr.GetLength(0); i++) 33 { 34 for (int j = 0; j < arr.GetLength(1); j++) 35 { 36 Console.Write(arr[i,j] + " "); 37 } 38 } 39 Console.ReadKey(); 40 } 41 } 42 }
系统会提示“索引超出了数组界限”。
在上面 的示例中,我们使用嵌套的for循环来分配二维数组元素。我们也可以在二维数组声明的时候给其赋值。
1 namespace TwoDimensionalArayDemo 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 //Assigning the array elements at the time of declaration 8 int[,] arr = {{11,12,13,14}, 9 {21,22,23,24}, 10 {31,32,33,34}}; 11 12 //printing values of array using for each loop 13 foreach (int i in arr) 14 { 15 Console.Write(i + " "); 16 } 17 Console.WriteLine("\n"); 18 19 //printing the values of array using nested for loop 20 for (int i = 0; i < arr.GetLength(0); i++) 21 { 22 for (int j = 0; j < arr.GetLength(1); j++) 23 { 24 Console.Write(arr[i, j] + " "); 25 } 26 } 27 28 Console.ReadKey(); 29 } 30 } 31 }
交错数组
这些也是二维数组,也将以行和列的形式存储数据。但是在这个交错的数组中,每一行的列大小是不同的。那就意味着如果第一行包含5列,第二行可能包含4列,而第三行可能包含10列。所以,你需要记住的一点就是,如果列的大小在每行发生变化,那么它就是一个交错数组。如果所有行中,列的大小保持不变,则它是一个矩形二维数组。
在C#中,交错数组也叫做数组中的数组。这是因为在交错数组的情况下,每一行都是一维数组。因此,在C#中,具有不同列大小的多个一维数组组合形成了一个交错数组。
语法: <type> [][] <name> = new <type> [rows][];
示例:
int [][] arr = new int[3][]; //Or int [][] arr = {list of values};
要在C#中声明交错数组,只需在声明时指定数组中所需要的行数即可。
示例如下:
int [][] arr = new int[4][];
在上面的数组声明中,我们指定在数组中需要4行。一旦你指定了你想要的数组的行数,那么你需要如下面所示那样用一维数组初始化每一行的列。
arr[0] = new int[5]; // we want five columns in the first row
arr[1] = new int[6]; // we want six columns in the first row
arr[2] = new int[4]; // we want four columns in the first row
arr[3] = new int[5]; // we want five columns in the first row
交错数组示例如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace TwoDimensionalArayDemo 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 int[][] arr = new int[4][]; 15 16 arr[0] = new int[5]; 17 arr[1] = new int[6]; 18 arr[2] = new int[4]; 19 arr[3] = new int[5]; 20 21 for (int i = 0; i < arr.GetLength(0); i++) 22 { 23 for (int j = 0; j < arr[i].Length; j++) 24 { 25 Console.Write(arr[i][j] + " "); 26 } 27 } 28 Console.WriteLine(); 29 30 for (int i = 0; i < arr.GetLength(0); i++) 31 { 32 for (int j = 0; j < arr[i].Length; j++) 33 { 34 Console.WriteLine("此处j值是{0}",j); 35 arr[i][j] = j++; 36 } 37 } 38 39 for (int i = 0; i < arr.GetLength(0); i++) 40 { 41 foreach (int x in arr[i]) 42 { 43 Console.Write(x + " "); 44 } 45 } 46 } 47 } 48 }
在上面的例子中,我们通过使用嵌套的for循环来给交错数组的元素赋值。在交错数组定义的时候,也可以给交错数组赋值。
1 namespace TwoDimensionalArayDemo 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 // Assigning the values of the jagged array 8 // at the time of its declaration 9 int[][] arr = { 10 new int[4]{11,12,13,14}, 11 new int[5]{21,22,23,24,25}, 12 new int[3]{31,32,33} 13 }; 14 15 //printing the values of jagged array by using nested for loop 16 for (int i = 0; i < arr.GetLength(0); i++) 17 { 18 for (int j = 0; j < arr[i].Length; j++) 19 { 20 Console.Write(arr[i][j] + " "); 21 } 22 } 23 Console.WriteLine(); 24 25 //print the values of jagged array by using foreach loop within for loop 26 for (int i = 0; i < arr.GetLength(0); i++) 27 { 28 foreach (int x in arr[i]) 29 { 30 Console.Write(x + " "); 31 } 32 } 33 Console.ReadKey(); 34 } 35 } 36 }
希望这篇文章能帮助到你!