C#基础-数组

 一维数组:

namespace 数组
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 基本概念
            //相同元素的集合,有一维、多维、交错数组等
            #endregion
            #region 数组的声明
            int[] ints1;
            int[] ints2 = new int[4];//声明有四个位置的数组,默认为0
            int[] ints3 = new int[4] { 1, 2, 3, 4 };
            int[] ints4 = new int[] { 1, 2, 3, 4 };
            int[] ints5 = { };
            #endregion
            #region 数组的使用
            int[] ints6 = { 1, 2, 3, 4, 5, 6 };
            //数组长度
            Console.WriteLine(ints6.Length);
            //获取元素
            Console.WriteLine(ints6[0]);
            #endregion
        }
    }
}

多维数组和交错数组:

#region 二维数组
//声明
int[,] arr1;
int[,] arr2 = new int[1, 2];
int[,] arr3 = new int[2, 2] { { 1, 2 }, 
                              { 1, 2 } };
int[,] arr4 = new int[,] { { 1, 2 },
                          { 1, 2 } };
int[,] arr5 ={ { 1, 2 },
               { 1, 2 } };
//多维数组长度
Console.WriteLine(arr5.GetLength(0));//得到行
Console.WriteLine(arr5.GetLength(1));//得到列
//获取元素
Console.WriteLine(arr5[0, 1]);
#endregion
#region 交错数组
//数组的数组,每个维度不同
//交错的声明
int[][] a1;
int[][] a2 = new int[3][];
int[][] a3 = new int[3][] { new int[]{1, 2, 3, 4},
                            new int[]{1, 2, 3},
                            new int[]{1, 2},};
//交错数组长度获取
Console.WriteLine(a3.GetLength(0));//获取行数
Console.WriteLine(a3[0].Length);//获取第1行的列数
//遍历交错数组
for(int i = 0; i < a3.GetLength(0); i++)
{
    for(int j = 0; j < a3[i].Length; j++)
    {
        Console.Write(a3[i][j] + " ");
    }
    Console.WriteLine();
}
#endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值