using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //交错数组 Console.WriteLine("交错数组"); int[][] arr = new int[3][] { new int[] { 1, 2, 3 }, new int[] { 4 }, new int[] { 5, 6 } }; PrintJaggedArry(arr); //二维数组 Console.WriteLine("\r\n二维数组"); int[,] arr2 = new int[3, 2] { { 1, 3 }, { 2, 4 }, { 2, 6 } }; Console.WriteLine($"元素总数:{arr2.Length}"); int len = arr2.GetLength(0); Console.WriteLine($"第1维长度:{len}"); int len2 = arr2.GetLength(1); Console.WriteLine($"第2维长度:{len2}"); for (int i = 0; i < len; i++) { for (int j = 0; j < len2; j++) { Console.Write($"{arr2[i, j]} "); } Console.WriteLine(); } //二维数组转交错数组 Console.WriteLine("\r\n二维数组转交错数组"); var arr3 = TwoDimenArryToJaggedArry(arr2); PrintJaggedArry(arr3); Console.ReadKey(); } private static void PrintJaggedArry(int[][] arr) { Console.WriteLine($"外层数组长度:{arr.Length}"); for (int i = 0; i < arr.Length; i++) { Console.Write($"子数组{i + 1}(长度:{arr[i].Length}): "); for (int j = 0; j < arr[i].Length; j++) { Console.Write(arr[i][j] + " "); } Console.WriteLine(); } } private static int[][] TwoDimenArryToJaggedArry(int[,] arr2) { int len = arr2.GetLength(0); int len2 = arr2.GetLength(1); int[][] arr3 = new int[len][]; for (int i = 0; i < len; i++) { arr3[i] = new int[len2]; for (int j = 0; j < len2; j++) { arr3[i][j] = arr2[i, j]; } } return arr3; } } }
标签:arr,Console,WriteLine,int,二维,arr2,数组,交错 From: https://www.cnblogs.com/chixiner/p/17656562.html