博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 语法练习(7): 数组
阅读量:6855 次
发布时间:2019-06-26

本文共 3110 字,大约阅读时间需要 10 分钟。

  hot3.png

字符串数组:

using System;class MyClass{    static void Main()    {        string[] arr = new string[3] { "aa", "bb", "cc" };        foreach (string s in arr) Console.WriteLine(s); // aa/bb/cc        Console.ReadKey();    }}
整数数组:

using System;class MyClass{    static void Main()    {        int[] arr = { 11, 22, 33 };        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}
初始化时维数可以省略; 若不省略, 得一致:

using System;class MyClass{    static void Main()    {        int[] arr = new int[3] { 11, 22, 33 };        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}
声明同时指定维数, 但暂不赋值:

using System;class MyClass{    static void Main()    {        int[] arr = new int[3];        foreach (int i in arr) Console.WriteLine(i); // 0/0/0        arr[0] = 11;        arr[1] = 22;        arr[2] = 33;        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}
先声明, 赋值时再确定维数:

using System;class MyClass{    static void Main()    {        int[] arr;        arr = new int[] { 11, 22, 33 };        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}
可改变声明时的维数:

using System;class MyClass{    static void Main()    {        int[] arr = new int[3];        arr = new int[4] { 11, 22, 33, 44 };        foreach (int i in arr) Console.WriteLine(i); // 11/22/33/44        Console.ReadKey();    }}
如果用变量做数组维数, 一定要是 const:

using System;class MyClass{    static void Main()    {        const int size = 3;        int[] arr = new int[size] { 11, 22, 33};        foreach (int i in arr) Console.WriteLine(i); // 11/22/33        Console.ReadKey();    }}
二维数组初始化:

using System;class MyClass{    static void Main()    {        int[,] arr = { {11,12,13,14}, {21,22,23,24}, {31,32,33,34} };        foreach (int i in arr) Console.WriteLine(i);        Console.ReadKey();    }}
二维数组赋值:

using System;class MyClass{    static void Main()    {        int[,] arr = new int[3, 4];        arr[0,0] = 11;        arr[0,1] = 12;        arr[0,2] = 13;        arr[0,3] = 14;        arr[1,0] = 21;        arr[1,1] = 22;        arr[1,2] = 23;        arr[1,3] = 24;        arr[2,0] = 31;        arr[2,1] = 32;        arr[2,2] = 33;        arr[2,3] = 34;        foreach (int i in arr) Console.WriteLine(i);        Console.ReadKey();    }}
多维数组:

using System;class MyClass{    static void Main()    {        int[,,] arr = new int[2, 3, 4];        for (int x = 0; x < 2; x++) for (int y = 0; y < 3; y++) for (int z = 0; z < 4; z++)            arr[x,y,z] = Convert.ToInt32(Convert.ToString(x+1) + Convert.ToString(y+1) + Convert.ToString(z+1));        foreach (int i in arr) Console.WriteLine(i);        Console.ReadKey();    }}
数组中的数组:

using System;class MyClass{    static void Main()    {        int[][] arr = new int[3][];        arr[0] = new int[2] { 11, 12 };        arr[1] = new int[3] { 21, 22, 23 };        arr[2] = new int[4] { 31, 32, 33, 34 };        foreach (int[] ns in arr) foreach (int n in ns)                Console.WriteLine(n);        Console.ReadKey();    }}

转载于:https://my.oschina.net/hermer/blog/319352

你可能感兴趣的文章
华为:缺省路由:默认路由 default route
查看>>
K均值聚类算法的MATLAB实现
查看>>
php中sql语句
查看>>
linux中MySQL小结
查看>>
浅谈以人为本
查看>>
Programmer10载身心成长历程回顾
查看>>
记一次springboot下maven工程方式导入pom.xml首行报错
查看>>
匿名内部类,Object类
查看>>
Tomcat(2)配置Tomcat的虚拟主机 、日志
查看>>
Linux环境下编译安装Mysql
查看>>
Rsync+Inotify
查看>>
LNMP架构搭建
查看>>
功能表单之子功能集合字段类型的使用—JEPLUS软件快速开发平台
查看>>
eyoucms 后台能不能上传视频跟播放?
查看>>
MYSQL的主从复制与读写分离
查看>>
为何 Go 的声明语法有点怪?(语法比较)
查看>>
Java中JVM的原理
查看>>
想来华为云实习&就业?硬核敲门砖送给你!
查看>>
备份镜像远程ftp文件夹的批处理
查看>>
企业落地Kubernetes的问题与对策
查看>>