您的位置:首页 > 新闻 > 会展 > 交错数组知识点

交错数组知识点

2024/10/5 14:53:14 来源:https://blog.csdn.net/Luo3255069063/article/details/139011396  浏览:    关键词:交错数组知识点

基本概念

交错数组是数组的数组,每个维度的数量可以不同。
注意:二维数组的每行的列数相同,交错数组每行的列数可能不同。

数组的申明

//变量类型[][] 交错数组名;
int[][] arr1;//变量类型[][] 交错数组名 = new 变量类型[行数][];
int[][] arr2 = new int[3][];//变量类型[][] 交错数组名 = new 变量类型[行数][]{ 一维数组1, 一维数组2,........ };
int[][] arr3 = new int[3][] { new int[] { 1, 2, 3 },new int[] { 1, 2 },new int[] { 1 }};//变量类型[][] 交错数组名 = new 变量类型[][]{ 一维数组1, 一维数组2,........ };
int[][] arr4 = new int[][] { new int[] { 1, 2, 3 },new int[] { 1, 2 },new int[] { 1 }};//变量类型[][] 交错数组名 = { 一维数组1, 一维数组2,........ };
int[][] arr5 = { new int[] { 1, 2, 3 },new int[] { 1, 2 },new int[] { 1 }};

数组的使用

数组的长度

//行
Console.WriteLine(array.GetLength(0));
//得到某一行的列数
Console.WriteLine(array[0].Length);

获取交错数组中的元素

// 注意:不要越界
Console.WriteLine(array[0][1]);

修改交错数组中的元素

array[0][1] = 99;
Console.WriteLine(array[0][1]);

遍历交错数组

for (int i = 0; i < array.GetLength(0); i++)
{for (int j = 0; j < array[i].Length; j++){Console.Write(array[i][j] + " ");}Console.WriteLine();
}

小结:

  1. 概念:交错数组可以存储同一类型的m行不确定列的数据。
  2. 一定要掌握的内容:申明、遍历、增删查改。
  3. 所有的变量类型都可以申明为交错数组。
  4. 一般交错数组很少使用,了解即可。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com