C# · 12月 20, 2021

C# — 交错数组的使用

C# — 交错数组的使用

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

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

Main([][] week = [] = [] { ,,,,,,] = [] { ,,,,,,] = [] { ,,,,,,</span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> i = <span style=”color: #800080″&gt;0</span>; i < week.Length; i++<span style=”color: #000000″&gt;){ Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;—————————————————————-</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); Console.Write(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;第</span><span style=”color: #800000″&gt;”</span> + (i + <span style=”color: #800080″&gt;1</span>).ToString() + <span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;个数组的值:</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> j = <span style=”color: #800080″&gt;0</span>; j < week[i].Length; j++<span style=”color: #000000″&gt;) { Console.Write(week[i][j] </span>+ <span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;,</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); } Console.WriteLine();}Console.ReadKey();

}

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

Main(</span><span style=”color: #0000ff”&gt;int</span>[][] number = <span style=”color: #0000ff”&gt;new</span> <span style=”color: #0000ff”&gt;int</span>[<span style=”color: #800080″&gt;3</span><span style=”color: #000000″&gt;][];number[</span><span style=”color: #800080″&gt;0</span>] = <span style=”color: #0000ff”&gt;new</span> <span style=”color: #0000ff”&gt;int</span>[] { <span style=”color: #800080″&gt;1</span>,<span style=”color: #800080″&gt;3</span>,<span style=”color: #800080″&gt;5</span>,<span style=”color: #800080″&gt;7</span>,<span style=”color: #800080″&gt;9</span><span style=”color: #000000″&gt; };number[</span><span style=”color: #800080″&gt;1</span>] = <span style=”color: #0000ff”&gt;new</span> <span style=”color: #0000ff”&gt;int</span>[] { <span style=”color: #800080″&gt;0</span>,<span style=”color: #800080″&gt;2</span>,<span style=”color: #800080″&gt;4</span>,<span style=”color: #800080″&gt;6</span>,<span style=”color: #800080″&gt;8</span><span style=”color: #000000″&gt; };number[</span><span style=”color: #800080″&gt;2</span>] = <span style=”color: #0000ff”&gt;new</span> <span style=”color: #0000ff”&gt;int</span>[] { <span style=”color: #800080″&gt;0</span>,<span style=”color: #800080″&gt;1</span>,<span style=”color: #800080″&gt;8</span>,<span style=”color: #800080″&gt;9</span><span style=”color: #000000″&gt; };</span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> i = <span style=”color: #800080″&gt;0</span>; i < number.Length; i++<span style=”color: #000000″&gt;){ Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;—————————————————————-</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); Console.Write(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;第</span><span style=”color: #800000″&gt;”</span> + (i + <span style=”color: #800080″&gt;1</span>).ToString() + <span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;个数组的值:</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> j = <span style=”color: #800080″&gt;0</span>; j < number[i].Length; j++<span style=”color: #000000″&gt;) { Console.Write(number[i][j] </span>+ <span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;,</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); } Console.WriteLine();}Console.ReadKey();

}

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

Main([][,] numberX = [] = [,] { { , },{ ,] = [,] { { ,,, },{ ,,] = [,] { { , },{ ,,</span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> i = <span style=”color: #800080″&gt;0</span>; i < numberX.Length; i++<span style=”color: #000000″&gt;){ Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;———-第</span><span style=”color: #800000″&gt;”</span> + (i + <span style=”color: #800080″&gt;1</span>).ToString() + <span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;个二维数组————————–</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> j = <span style=”color: #800080″&gt;0</span>; j < Math.Sqrt(numberX[i].Length); j++<span style=”color: #000000″&gt;) { </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> k = <span style=”color: #800080″&gt;0</span>; k < Math.Sqrt(numberX[i].Length); k++<span style=”color: #000000″&gt;) { Console.Write(numberX[i][j,k] </span>+ <span style=”color: #800000″&gt;”</span> <span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); } Console.WriteLine(); }}Console.ReadKey();

}