学习c#第十四天(数组之二维数组)

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

namespace ArraryStruct
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //1.二维数组基本语法
            //1.声明与初始化
            //声明:数据类型[,] 数组名;
            //初始化:数组名 = new 数据类型[行数,列数]
            //声明,初始化二合一格式:
            //数据类型[,] 数组名 = new 数据类型[行数,列数];
            /*
            //声明.
            int[] intArray;
            int[,] intArray_2;

            //初始化.
            intArray = new int[5];  //5个存储空间
            intArray_2 = new int[3, 3]; //3*3 =9 个存储空间. 行数 列表.

            //声明初始化二合一:
            int[] intOne = new int[8];
            int[,] intTwo = new int[6, 9]; //6行 9列

            //赋值与取值
            //赋值:数组名[下标,下标] = 值;
            //取值:数组名[下标,下标]; 

            //第一行.
            intArray_2[0, 0] = 1;
            intArray_2[0, 1] = 2;
            intArray_2[0, 2] = 3;


            //第二行.
            intArray_2[1, 0] = 11;
            intArray_2[1, 1] = 22;
            intArray_2[1, 2] = 33;

            //第三行.
            intArray_2[2, 0] = 111;
            intArray_2[2, 1] = 222;
            intArray_2[2, 2] = 333;

            Console.WriteLine(intArray_2[1, 1]);
            Console.ReadKey();
            Console.ReadKey();

            //声明初始化赋值三合一:
            //数据类型[,] 数组名 = new 数据类型 [,]{
            //   {0,0,0,0,0,0,0,0,0,0}
            //   {1,1,1,1,1,1,1,1,1}

            //}
            */
            //三合一
            /*
            int[,] intArrays = new int[,] { 
                { 1, 2, 3 }, 
                { 11, 22, 33 }, 
                { 111, 222, 333 }, 
                { 1111, 2222, 3333 } 
            };

            //二维数组遍历.
            for (int i =0; i < intArrays.GetLength(0); i++)
            {
                for(int j = 0;j< intArrays.GetLength(1); j++)
                {
                    Console.Write(intArrays[i, j] + "\t");
                }
                Console.WriteLine();
            }

            Console.ReadKey();
            */
            //2.二维数组的遍历
            //for 循环嵌套
            //数组名.GetLength(轴向下标);可以获取某个轴向的元素个数。

            //3.二维数组在游戏中的应用
            //存储游戏地图
            //例:迷宫游戏地图实现

            //二维数组存储迷宫地图.
            /*
            int[,] gameMaps = new int[,]
            {
                { 1,1,1,1,1,1,1,1,1,1,1,1,1},
                { 1,0,0,0,0,0,1,0,0,1,0,0,1},
                { 1,0,0,0,0,0,0,0,0,0,0,0,1},
                { 1,0,0,0,1,0,0,0,0,1,0,0,1},
                { 1,0,0,0,1,0,0,0,0,1,0,0,1},
                { 1,0,0,0,1,0,0,0,0,1,0,0,1},
                { 1,1,1,1,1,1,1,1,1,1,1,1,1}
            };

            //遍历二维数组生成地图.
            for(int i = 0;i < gameMaps.GetLength(0); i++)
            {
                for(int j = 0; j < gameMaps.GetLength(1); j++)
                {
                    if (gameMaps[i,j] == 1)
                    {
                        Console.Write("■");
                    }
                    if (gameMaps[i, j] == 0)
                    {
                        Console.Write("□");
                    }
                }
                Console.WriteLine();
            }
            Console.ReadKey();
            */
        }
    }
}


 

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值