C# —— 二维数组

C#当中 多维数组 又称为矩形数组,最简单的多维数组是二维数组,可以被认为带有x行和y列的表格

二维数组的定义: 不是ArrayList, ArrayList没有多维情况, 多维数组指的是Array 静态定义方式

类型[,]名字 = new 类型[行数,列数]

int[,]ints = new int[3, 4];//3行4列的数据

string[,] strings = new string[5, 4];

初始化多维数组

int[,] ints1 = new int[3, 4]

{

{ 1,2,3,4},// 第0行

{ 4,5,6,7},// 第一行

{ 1,2,4,4}// 第二行

};

会根据初始值 进行推导

cs 复制代码
string[,] strings1 = new string[,]
{
    { "a","b","c"},
    { "a","b","c"},
    { "a","b","c"}
};

int[,,] i1 = new int[,,]
{
    {
        { 1,2,3,4 },
        { 1,2,3,4 },
    },
    {
        { 1,2,3,4 },
        { 1,2,3,4 },
    }
};

也可以通过for循环来进行添加元素

cs 复制代码
string[,] s1 = new string[5, 10];
for (int i = 0; i < 5; i++)// 控制行数
{
    for (int j = 0; j < 10; j++)//控制列数
    {
        s1[i, j] = i + "," +j; //
    }
}

二维数组取值 去索引值为3行 索引值为6的列

Console.WriteLine(s1[3,6]); // 3 6

Console.WriteLine(s1[5, 9]);

遍历二维数组

cs 复制代码
foreach (string i in s1)
{
    // 打印所有的元素
    Console.WriteLine(i);
}
// 数组的总长度
Console.WriteLine(s1.Length+"......");
Console.WriteLine(s1.GetLength(0));// 几行
Console.WriteLine(s1.GetLength(1));// 几列

for循环遍历

cs 复制代码
 for (int i = 0; i < s1.GetLength(0); i++)
 {
     for (int j = 0; j < s1.GetLength(1); j++)
     {
         Console.WriteLine(s1[i,j]+"------");
     }
 }

 Console.ReadLine();
相关推荐
csbysj20201 天前
Perl 格式化输出
开发语言
tao3556671 天前
【Python刷力扣hot100】42. Trapping Rain Water
开发语言·python·leetcode
Miraitowa_cheems1 天前
LeetCode算法日记 - Day 88: 环绕字符串中唯一的子字符串
java·数据结构·算法·leetcode·深度优先·动态规划
消失的旧时光-19431 天前
Kotlin 协程最佳实践:用 CoroutineScope + SupervisorJob 替代 Timer,实现优雅周期任务调度
android·开发语言·kotlin
错把套路当深情1 天前
Kotlin保留小数位的三种方法
开发语言·python·kotlin
B站_计算机毕业设计之家1 天前
python电商商品评论数据分析可视化系统 爬虫 数据采集 Flask框架 NLP情感分析 LDA主题分析 Bayes评论分类(源码) ✅
大数据·hadoop·爬虫·python·算法·数据分析·1024程序员节
wangnaisheng1 天前
【C#】EventHandler的使用
c#
小白菜又菜1 天前
Leetcode 1518. Water Bottles
算法·leetcode·职场和发展
长存祈月心1 天前
Rust Option 与 Result深度解析
算法
赵谨言1 天前
基于Python Web的大数据系统监控平台的设计与实现
大数据·开发语言·经验分享·python