当前位置:网站首页>C#多维数组的属性获取方法及操作注意
C#多维数组的属性获取方法及操作注意
2022-07-02 09:32:00 【chengjl8】
C#提供了多维数组的各种属性获取方法,但是其表达形式与我们的习惯不尽相同,因此,也是花了一些脑筋,写了一些代码测试才搞明白的,现分享给大家:
1、一维数组
一维数组是一个一行多列的数据组合,待会在二维数组里说明。
2、二维数组
我们定义一个如下二维数组:
double[,] array1 = new double[3, 6];//2维数组
其形象结构如下,这里:维数=2;第1个维度是行,这里为3;第二个维度是每行的列数,这里为6。
0 | 1 | 2 | 3 | 4 | 5 | |
第0行 | ||||||
第1行 | ||||||
第2行 |
这里,用代码获取数组的属性及结果如下(索引都是从0开始):
double[,] array1 = new double[3, 6];//2维数组
int a = array1.Rank;//维数=2
a = array1.GetLength(0);//获取第0维的元素个数=3
a = array1.GetLength(1);//获取第1维的元素个数=6
a = array1.GetLowerBound(0);//获取第0维的第一个元素索引=0
a = array1.GetUpperBound(0);//获取第0维的最后一个元素索引=2
a = array1.GetLowerBound(1);//获取第0维的第一个元素索引=0
a = array1.GetUpperBound(1);//获取第1维的最后一个元素索引=5
a = array1.Length;//获取总元素数量=18
3、三维数组
我们定义一个如下三维数组:
double[,,] array2 = new double[2, 3, 6];//3维数组
其形象结构如下,这里:维数=3;第1个维度层,这里为2,第2个维度是行,这里为3;第3个维度是每行的列,这里为6。
double[,,] array2 = new double[2, 3, 6];//3维数组
a = array2.Rank;//维数=3
a = array2.GetLength(0);//获取第0维的元素个数=2
a = array2.GetLength(1);//获取第1维的元素个数=3
a = array2.GetLength(2);//获取第2维的元素个数=6
a = array2.Length;//获取总元素数量=36
4、更多维数
更多维数也是如此理解,只不过是在定义里多加几个逗号而已[,,,,,,,],但是要注意:元素个数是指数级上升的。
5、操作注意
- 数组的操作比列表快很多,如果可能,就用这种数组结构。
- 遍历数组的高效方法是多层for循环,维序号越高的越要放内循环(见下),颠倒过来则效率降低:
for(int i = 0; i < 3; i++) { for(int j = 0; j < 6; j++) { array1[i, j] = -1; } }
边栏推荐
- 实验电镜距离测量之Matlab处理
- Special topic of binary tree -- acwing 1589 Building binary search tree
- ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
- QT learning diary 8 - resource file addition
- PCL projection point cloud
- 二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)
- K-d tree and octree of PCL
- Use Huawei performance management service to configure the sampling rate on demand
- spritejs
- III Chip startup and clock system
猜你喜欢
Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
How to transfer event objects and user-defined parameters simultaneously in Huawei express applications
一.STM32的开发环境,keil5/MDK5.14安装教程(附下载链接)
QT learning diary 8 - resource file addition
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
VS2019代码中包含中文内容导致的编译错误和打印输出乱码问题
【深入浅出玩转FPGA学习2----设计技巧(基本语法)】
webauthn——官方开发文档
[play with FPGA learning 4 in simple terms ----- talk about state machine design]
随机推荐
[in simple terms, play with FPGA learning 3 ----- basic grammar]
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
Special topic of binary tree -- acwing 1589 Building binary search tree
Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
The difference between self and static in PHP in methods
计算序列之和
TIPC介绍1
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
Indexer in C #
Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)
二叉树专题--AcWing 1589. 构建二叉搜索树
Iii. Système de démarrage et d'horloge à puce
Primary key policy problem
Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
[play with FPGA learning 4 in simple terms ----- talk about state machine design]
Leetcode 182 Find duplicate email (2022.07.01)
洛谷 P3398 仓鼠找 sugar(树上倍增 lca 判断树中两条路径是否相交 结论)
VS2019代码中包含中文内容导致的编译错误和打印输出乱码问题
Tick Data and Resampling