数组是一个存储相同类型元素的固定大小的顺序集合。数组是用来存储数据的集合,通常认为数组是一个同一类型变量的集合。
声明数组变量并不是声明 number0、number1、...、number99 一个个单独的变量,而是声明一个就像 numbers 这样的变量,然后使用 numbers[0]、numbers[1]、...、numbers[99] 来表示一个个单独的变量。数组中某个指定的元素是通过索引来访问的。
所有的数组都是由连续的内存位置组成的。最低的地址对应第一个元素,最高的地址对应最后一个元素。
在 C# 中声明一个数组,您可以使用下面的语法:
csharpdatatype[] arrayName;
其中,
例如:
csharpdouble[] balance;
声明一个数组不会在内存中初始化数组。当初始化数组变量时,您可以赋值给数组。数组是一个引用类型,所以您需要使用 new 关键字来创建数组的实例。
例如:
csharpdouble[] balance = new double[10];
您可以通过使用索引号赋值给一个单独的数组元素,比如:
csharpdouble[] balance = new double[10];
balance[0] = 4500.0;
csharp//您可以在声明数组的同时给数组赋值,比如:
double[] balance = { 2340.0, 4523.69, 3421.0};
//您也可以创建并初始化一个数组,比如:
int [] marks = new int[5] { 99, 98, 92, 97, 95};
//在上述情况下,你也可以省略数组的大小,比如:
int [] marks = new int[] { 99, 98, 92, 97, 95};
//您也可以赋值一个数组变量到另一个目标数组变量中。在这种情况下,目标和源会指向相同的内存位置:
int [] marks = new int[] { 99, 98, 92, 97, 95};
int[] score = marks;
当您创建一个数组时,C# 编译器会根据数组类型隐式初始化每个数组元素为一个默认值。例如,int 数组的所有元素都会被初始化为 0。
元素是通过带索引的数组名称来访问的。这是通过把元素的索引放置在数组名称后的方括号中来实现的。例如:
csharpdouble salary = balance[9];
下面是一个实例,使用上面提到的三个概念,即声明、赋值、访问数组:
csharpusing System;
namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
int [] n = new int[10]; /* n 是一个带有 10 个整数的数组 */
int i,j;
/* 初始化数组 n 中的元素 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100;
}
/* 输出每个数组元素的值 */
for (j = 0; j < 10; j++ )
{
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
Console.ReadKey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109
在前面的实例中,我们使用一个 for 循环来访问每个数组元素,您也可以使用一个 foreach语句来遍历数组。
以下实例我们使用 foreach 来遍历一个数组:
csharpusing System;
namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
int [] n = new int[10]; /* n 是一个带有 10 个整数的数组 */
/* 初始化数组 n 中的元素 */
for ( int i = 0; i < 10; i++ )
{
n[i] = i + 100;
}
/* 输出每个数组元素的值 */
foreach (int j in n )
{
int i = j-100;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
Console.ReadKey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109
交错数组是一个数组,其元素是数组,大小可能不同。 交错数组有时称为“数组的数组”。其元素是引用类型,初始化为null。 以下示例说明如何声明、初始化和访问交错数组。 第一个示例jaggedArray在一个语句中声明。 每个包含的数组在后续语句中创建。 第二个示例jaggedArray2在一个语句中声明和初始化。 可以混合使用交错数组和多维数组。 最终示例jaggedArray3是一维交错数组声明和初始化,包含大小不同的三个二维数组元素。
csharp
int[][] jaggedArray = new int[3][];
jaggedArray[0] = [1, 3, 5, 7, 9];
jaggedArray[1] = [0, 2, 4, 6];
jaggedArray[2] = [11, 22];
int[][] jaggedArray2 =
[
[1, 3, 5, 7, 9],
[0, 2, 4, 6],
[11, 22]
];
// Assign 77 to the second element ([1]) of the first array ([0]):
jaggedArray2[0][1] = 77;
// Assign 88 to the second element ([1]) of the third array ([2]):
jaggedArray2[2][1] = 88;
int[][,] jaggedArray3 =
[
new int[,] { {1,3}, {5,7} },
new int[,] { {0,2}, {4,6}, {8,10} },
new int[,] { {11,22}, {99,88}, {0,9} }
];
Console.Write("{0}", jaggedArray3[0][1, 0]);
Console.WriteLine(jaggedArray3.Length);
必须先初始化交错数组的元素,然后才能使用它们。 每个元素本身都是数组。 还可以使用初始值设定项填充数组元素的值。 使用初始值设定项时,无需数组大小。
本例生成一个数组,该数组的元素为数组自身。 每一个数组元素都有不同的大小。
csharp// Declare the array of two elements.
int[][] arr = new int[2][];
// Initialize the elements.
arr[0] = [1, 3, 5, 7, 9];
arr[1] = [2, 4, 6, 8];
// Display the array elements.
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
/* Output:
Element(0): 1 3 5 7 9
Element(1): 2 4 6 8
*/
交错数组是数组的数组。交错数组是一维数组。您可以声明一个带有 int 值的交错数组 scores,如下所示:
csharpint [][] scores;
声明一个数组不会在内存中创建数组。创建上面的数组:
csharpint[][] scores = new int[5][];
for (int i = 0; i < scores.Length; i++)
{
scores[i] = new int[4];
}
您可以初始化一个交错数组,如下所示:
csharpint[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};
其中,scores 是一个由两个整型数组组成的数组 -- scores[0] 是一个带有 3 个整数的数组,scores[1] 是一个带有 4 个整数的数组。下面的实例演示了如何使用交错数组:
csharpusing System;
namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
/* 一个由 5 个整型数组组成的交错数组 */
int[][] a = new int[][]{new int[]{0,0},new int[]{1,2},
new int[]{2,4},new int[]{ 3, 6 }, new int[]{ 4, 8 } };
int i, j;
/* 输出数组中每个元素的值 */
for (i = 0; i < 5; i++)
{
for (j = 0; j < 2; j++)
{
Console.WriteLine("a[{0}][{1}] = {2}", i, j, a[i][j]);
}
}
Console.ReadKey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
a[0][0] = 0 a[0][1] = 0 a[1][0] = 1 a[1][1] = 2 a[2][0] = 2 a[2][1] = 4 a[3][0] = 3 a[3][1] = 6 a[4][0] = 4 a[4][1] = 8
C# 支持多维数组。多维数组又称为矩形数组,像excel,就是一个标准的二维数组。
csharpstring[,] names;
int[,] ages;
多维数组最简单的形式是二维数组。一个二维数组,在本质上,是一个一维数组的列表。一个二维数组可以被认为是一个带有 x 行和 y 列的表格。下面是一个二维数组,包含 3 行和 4 列:
![[Csharp-Char类使用说明-01.png]]
数组中的每个元素是使用形式为 a[ i , j ] 的元素名称来标识的,其中 a 是数组名称,i 和 j 是唯一标识 a 中每个元素的下标。
多维数组可以通过在括号内为每行指定值来进行初始化。
csharp//声明一个行为3,列为4的二维数组
int[,] a = new int[3, 4] {
{0, 1, 2, 3} , /* 初始化索引号为 0 的行 */
{4, 5, 6, 7} , /* 初始化索引号为 1 的行 */
{8, 9, 10, 11} /* 初始化索引号为 2 的行 */
};
//其它几种声明初使化
int[,] b = new int[2, 4];//行2,4列数
int[][] c = new int[2][];//这时申明不能指定列数,只能每行指定列数
c[0]=new int[2];//第一行2列
c[1]=new int[3];//第二行3列
c[2]=new int[4];//第3行4列
int[,] d = new int[2, 2];
d[0, 0] = 1;
d[0, 1] = 2;
d[1, 0] = 3;
d[1, 1] = 4;
csharp/* 一个带有 5 行 2 列的数组 */
int[,] a = new int[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 } };
int i, j;
/* 输出数组中每个元素的值 */
for (i = 0; i < 5; i++)
{
for (j = 0; j < 2; j++)
{
Console.WriteLine($"a[{i},{j}] = {a[i,j]}");
}
}
Console.ReadKey();
将二维数组中数据行列数据调换位置
csharpint[,] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Console.WriteLine("原数组");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"{a[i, j]} ");
}
Console.WriteLine();
}
Console.WriteLine("转换后数组");
int tmp;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < i; j++)
{
tmp = a[i, j];
a[i, j] = a[j, i];
a[j, i] = tmp;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"{a[i, j]} ");
}
Console.WriteLine();
}
在 C# 中,数组是非常重要的,且需要了解更多的细节。下面列出了 C# 程序员必须清楚的一些与数组相关的重要概念:
| 概念 | 描述 |
|---|---|
| [多维数组] | C# 支持多维数组。多维数组最简单的形式是二维数组。 |
| [交错数组] | C# 支持交错数组,即数组的数组。 |
| [传递数组给函数] | 您可以通过指定不带索引的数组名称来给函数传递一个指向数组的指针。 |
| [参数数组] | 这通常用于传递未知数量的参数给函数。 |
| [Array 类] | 在 System 命名空间中定义,是所有数组的基类,并提供了各种用于数组的属性和方法。 |
本文作者:ZeroEngineer
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!