C# -- 交错数组的使用

C# -- 交错数组的使用,第1张

概述C# -- 交错数组的使用交错数组是元素为数组的数组。交错数组元素的维度和大小可以不同。交错数组有时称为“数组的数组”。1. 举例一:子数组是长度相同的一维数组static void Main(string[] args){string[][] week = new string[3][];week[0] = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };week[1] = new string[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };week[2] = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };for (int i = 0; i < week.Length; i++){Console.WriteLine("----------------------------------------------------------------");Console.Write("第" + (i + 1).ToString() + "个数组的值:");for (int j = 0; j < week[i].Length; j++){Console.Write(week[i][j] + ",");}Console.WriteLine();}Console.ReadKey();} 2. 举例二:子数组是长度不同的一维数组static void Main(string[] args){int[][] number = new int[3][];number[0] = new int[] { 1, 3, 5, 7, 9 };number[1] = new int[] { 0, 2, 4, 6, 8 };number[2] = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };for (int i = 0; i < number.Length; i++){Console.WriteLine("----------------------------------------------------------------");Console.Write("第" + (i + 1).ToString() + "个数组的值:");for (int j = 0; j < number[i].Length; j++){Console.Write(number[i][j] + ",");}Console.WriteLine();}Console.ReadKey();} 3. 举例三:子数组是长度不同的二维数组static void Main(string[] args){string[][,] numberX = new string[3][,];numberX[0] = new string[2, 2] { { "A", "A" }, { "B", "B" } };numberX[1] = new string[3, 3] { { "A", "A", "A" }, { "B", "B", "B" }, { "C", "C", "C" } };numberX[2] = new string[4, 4] { { "A", "A", "A", "A" }, { "B", "B", "B", "B" }, { "C", "C", "C", "C" }, { "D", "D", "D", "D" } };for (int i = 0; i < numberX.Length; i++){Console.WriteLine("----------第" + (i + 1).ToString() + "个二维数组--------------------------");for (int j = 0; j < Math.Sqrt(numberX[i].Length); j++){for (int k = 0; k < Math.Sqrt(numberX[i].Length); k++){Console.Write(numberX[i][j, k] + " ");}Console.WriteLine();}}Console.ReadKey();}

C# -- 交错数组的使用

交错数组是元素为数组的数组。交错数组元素的维度和大小可以不同。交错数组有时称为“数组的数组”。

1. 举例一:子数组是长度相同的一维数组

Main([][] week = [] = [] { ,,,,,,] = [] { ,,,,,,] = [] { ,,,,,,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>; i < week.Length; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;){ Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;----------------------------------------------------------------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;第</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span> + (i + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;1</span>).ToString() + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;个数组的值:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> j = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>; j < week[i].Length; j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;) { Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write(week[i][j] </span>+ <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); } Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne();}Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.ReadKey();

}

2. 举例二:子数组是长度不同的一维数组

Main(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span>[][] number = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span>[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;3</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;][];number[</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>] = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span>[] { <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;1</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;3</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;5</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;7</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;9</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; };number[</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;1</span>] = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span>[] { <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;2</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;4</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;6</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;8</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; };number[</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;2</span>] = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span>[] { <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;1</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;8</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;9</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; };</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>; i < number.Length; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;){ Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;----------------------------------------------------------------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;第</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span> + (i + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;1</span>).ToString() + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;个数组的值:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> j = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>; j < number[i].Length; j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;) { Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write(number[i][j] </span>+ <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); } Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne();}Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.ReadKey();

}

3. 举例三:子数组是长度不同的二维数组

Main([][,] numberX = [] = [,] { { , },{ ,] = [,] { { ,,, },{ ,,] = [,] { { , },{ ,,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>; i < numberX.Length; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;){ Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;----------第</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span> + (i + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;1</span>).ToString() + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;个二维数组--------------------------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> j = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>; j < Math.Sqrt(numberX[i].Length); j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> k = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>; k < Math.Sqrt(numberX[i].Length); k++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;) { Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write(numberX[i][j,k] </span>+ <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); } Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(); }}Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.ReadKey();

}

总结

以上是内存溢出为你收集整理的C# -- 交错数组的使用全部内容,希望文章能够帮你解决C# -- 交错数组的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264222.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存