二维数组与交叉数组小记

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

namespace 二维数组
{
    class Program
    {
        static void Main(string[] args)
        {
            //二维数组
            int[,] str1 = new int[3, 2] {{ 1, 1 },{ 2, 2 }, { 3, 3 }};
            //获取数组行数
            Console.WriteLine(str1.GetLength(0));
            //获取数组列数
            Console.WriteLine(str1.GetLength(1));
            //获取数组元素个数
            Console.WriteLine(str1.Length);
            Console.WriteLine("二维数组元素输出:");
            for (int i = 0; i < str1.GetLength(0); i++)
            {
                for (int k = 0; k < str1.GetLength(1); k++)
                {
                    if (k == str1.GetLength(1) - 1)
                    {
                        Console.WriteLine(str1[i, k].ToString());
                    }
                    else
                    {
                        Console.Write(str1[i,k].ToString() + ",");
                    }
                }
            }
            
            //交叉数组,可以理解为其是一种特殊的一维数组,即存储一维数组的数组
            int[][] str2 = new int[][] { new int[] { 1, 2, 3 }, new int[] { 1 } };
            for (int i = 0; i < str2.Length; i++)
            {
                Console.WriteLine("交叉数组第[" + i + "]行开始输出:");
                for (int k = 0; k < str2[i].Length; k++)
                {
                    if (k == str2[i].Length - 1)
                    {
                        Console.WriteLine(str2[i][k].ToString());
                    }
                    else
                    {
                        Console.Write(str2[i][k].ToString() + ",");
                    }
                }
            }
            Console.Read();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值