C# -- 交错数组的使用
交错数组是元素为数组的数组。交错数组元素的维度和大小可以不同。交错数组有时称为“数组的数组”。
1. 举例一:子数组是长度相同的一维数组
Main([][] week = [] = [] { ,,,,,,] = [] { ,,,,,,] = [] { ,,,,,,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>; i < week.Length; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">){ 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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">----------------------------------------------------------------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); 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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">第</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span> + (i + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span>).ToString() + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">个数组的值:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> j = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>; j < week[i].Length; j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) { 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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } 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">int</span>[][] number = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span>[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">3</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">][];number[</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>] = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span>[] { <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">3</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">5</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">7</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">9</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> };number[</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span>] = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span>[] { <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">2</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">4</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">6</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">8</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> };number[</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">2</span>] = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span>[] { <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">8</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">9</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> };</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>; i < number.Length; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">){ 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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">----------------------------------------------------------------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); 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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">第</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span> + (i + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span>).ToString() + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">个数组的值:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> j = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>; j < number[i].Length; j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) { 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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">,</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } 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">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>; i < numberX.Length; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">){ 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">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">----------第</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span> + (i + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span>).ToString() + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">个二维数组--------------------------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> j = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>; j < Math.Sqrt(numberX[i].Length); j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> k = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>; k < Math.Sqrt(numberX[i].Length); k++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) { 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">"</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } 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# -- 交错数组的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)