C# 数组、一维数组、二维数组、多维数组、锯齿数组

时间:2023-09-26 00:01:44

C#  数组、一维数组、二维数组、多维数组、锯齿数组

一.数组:

如果需要使用同一类型的对象,就可以使用数组,数组是一种数据结构,它可以包含同一类型的多个元素。它的长度是固定的,如长度未知的情况下,请使用集合。

二.一维数组:

  声明及初始化:

  class Program
{
static void Main(string[] args)
{
//方法一
int[] num = new int[];//声明一个长度为3的值类型的数组;
num[] = ;//为数组赋值;
num[] = ;
num[] = ; //方法二
int[] num1 = new int[] { , , };//声明一个长度为3的值类型的数组并为其赋值;
}
}

三.二维数组:

  声明及初始化:

 class Program
{
static void Main(string[] args)
{
//方法一:
int[,] num =new int[,];
num[, ] = ;
num[, ] = ;
num[, ] = ;
num[, ] = ;
num[, ] = ;
num[, ] = ;
num[, ] = ;
num[, ] = ;
num[, ] = ; //方法二:
int[,] num1 = { { , , }, { , , }, { , , } };
}
}

图更容易理解:坐标观。【先列后排】【索引】
C#  数组、一维数组、二维数组、多维数组、锯齿数组

四.多维数组:

class Program
{
static void Main(string[] args)
{
int[,,] num =
{
{{,},{,}}
,{{,},{,}}
,{{,},{,}}
};
}
}

N维的原理相同;

五.锯齿数组:

在声明锯齿数组时,要依次放置左右括号。在初始化锯齿数组时,只在第一对方括号中设置数组包含的行数。定义各行中元素个数的第2个方括号设置为空,因为这类数组的每一行包含不同的元素个数。之后,为每一行指定行中的元素个数:

 class Program
{
static void Main(string[] args)
{
int [][]num=new int[][];
num[]=new int[]{,};
num[] = new int[] { ,,,,};
num[] = new int[] { ,, }; }
}