C# 数组

2.1数组的概念

        数组是具有相同数据类型的一组数据的集合。数组中的每一个变量称为数组的元素,数组能够容纳元素的数量称为数组的长度。
        int[ ]  a =  new int[80];
        数据类型  标识符   [正整数常量];
        数组元素的基本数据类型      数组名     也可以是一个常量表达式

2.2一维数组

1、语法
        数组元素类型 [ ]   数组名字;
        int[  ]arr;
        string[ ]arr;
2、初始化一维数组
        初始化:就是给数组赋初值。
        三种初始化方法
        (1)int[]a= new int[3];
                 a[0]=7;          //第一个元素从0开始
                 a[1]=8;
                 a[2]=9;
        (2)int[ ]b=gew int[ ]{4,5,6};
        (3)int[ ]c={1,2,3};

4、内存中的数组存储形式
        int[ ]a = new int[3];
        a[0]=7;
        a[1]=8;
        a[2]=9;

f236fe0fb82d468fb90a04f56ffa74d6.jpeg

84edfddc01e0469697cfd23588df368b.jpeg

注:int[ ]array=hew int[(5]{0,1,2);    //声明并初始化一维数组
        代码错误  初始值的个数必须与数组的长度一样!

例:使用一维数组输出每个月的天数。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            for (int i = 0; i < 12; i++)
            {
                Console.WriteLine((i + 1) + "月有" + arr[i] + "天");
            }
            Console.Read();
        }
    }
}

943c86753ff64d00ba6539b0ec9df7e1.png

 2.3二维数组

1、语法
        数据类型[ , ]数组名;
        数据类型[ ][ ]数组名;
        int[ , ]arr;
        string[ ][ ] arr;
2、初始化二维数组
        三种方法
        (1)int[ , ]myarr = new int[2,2];
             myarr[0,0]=0;
             myarr[0,1]=1;
             myarr[1,0]=1;
             myarr[1,1]=2;
        (2)int[ ][ ]myarr=new int[2][];
             myarr[0]=new int[ ]{0,1};
             myarr[1]=new int[ ]{1,2};

(3)int[ , ]myarr =new int[2,2]{{12,0},{45,10}};或
         int[ , ] myarr=new int[ , ]{{12,0 },{45,10 }};或
         int[ , ]myarr ={{12,0},{45,10}};

例:将二维数组中行数据和列数据调换位置。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr = { { 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.WriteLine(arr[i,j] + " ");
                }
                Console.WriteLine();
            }
            int temp;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    temp = arr[i,j];
                    arr[i,j] = arr[j,i];
                    arr[j,i] = temp;
                }
            }
            Console.WriteLine("--------调换之后的数组--------");
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.WriteLine(arr[i,j] + " ");
                }
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}

6bc5cee15ff24beeaf51d84397ed3cf3.pngf9f166854d334faeae1d640f78cdc980.jpeg

2.4不规则数组

int[ ][ ]a=new int[3][ ];
a[0]=new int[5];
a[1]=new int[3];
a[2]=new int[4];

e3a372deba1f4cdf864cc8a77e99af7d.jpeg

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值