unity3D-游戏/AR/VR在线就业班 C#入门二维数组学习笔记

来源:互联网 发布:excel同列不同数据分列 编辑:程序博客网 时间:2024/05/16 00:54
unity3D-游戏/AR/VR在线就业班 C#入门二维数组学习笔记
点击观看视频学习:http://edu.csdn.NET/lecturer/107

二维数组

有两个下标的数组叫做二维数组

类似[,]数组名=new类型[常量表达式1,常量表达式2]

int[,] numbers= new int[2,3];

[0,0] [0,1] [0,2]

[1,0] [1,1] [1,2]

举例说明

sing System;namespace Lesson16{    class MainClass    {        public static void Main (string[] args)        {            //声明并初始化 int类型二维数组numbers            //一个2行3列的数组            int[,] numbers= new int[2,3];            numbers[0,1]=3;            Console.WriteLine (numbers[1,2]);        }    }}


举例说明

using System;namespace Lesson16{    class MainClass    {        public static void Main (string[] args)        {            //声明并初始化 int类型二维数组numbers            //一个2行3列的数组            int[,] numbers = new int[2, 3] {                {1,2,3},                {4,5,6}            };            numbers[0,1]=3;            Console.WriteLine (numbers[1,2]);        }    }}

遍历出二维数组的元素:

using System;namespace Lesson16{    class MainClass    {        public static void Main (string[] args)        {            //声明并初始化 int类型二维数组numbers            //一个2行3列的数组            int[,] numbers = new int[2, 3] {                {1,2,3},                {4,5,6}            };//            numbers[0,1]=3;            Console.WriteLine (numbers[1,2]);            //使用循环遍历数组            //需要两个for循环嵌套            //外层:遍历数组中每一行            //内层:一次遍历某行数据中的每个元素            for (int i = 0; i < 2; i++) {                for (int j= 0;j< 3; j++) {                                        Console.WriteLine (numbers[i,j]);                    }            }        }    }}             //foreach遍历每一个元素            //遍历整个numbers数组,依次获取里面的int类型元素            //当我们不需要对循环本身的整个过程进行控制,只需要关注数组中每个元素的时候,可以使用foreach            foreach (int num in numbers) {                                Console.WriteLine (num);                }





0 0
原创粉丝点击