锯齿状阵列的通用打印(Generic printing of jagged arrays)

编程入门 行业动态 更新时间:2024-10-18 03:30:31
锯齿状阵列的通用打印(Generic printing of jagged arrays)

我目前正在处理大量数组,为了调试目的,我编写了一个通用的Print()方法来打印不同类型的数组

static void Main() { Print(new double[]{ 1, 2, 3 }); // Output: [ 1, 2, 3 ] } static void Print<T>(T[] array) { int size = array.Length; if (size == 0) return; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); }

到目前为止工作正常。 然后我想打印一个数组数组,如double[][] ,并尝试以下方法:

static void Main() { Print(new double[][] { new double[] { 1, 2 }, new double[] { 3, 4 }, new double[] { 5, 6 }, }); // Output: [ 1, 2 ] // [ 3, 4 ] // [ 5, 6 ] } static void Print<T>(T[] array) { if (array.Length == 0) return; if (array[0].GetType().IsArray) { foreach (var element in array) { Print<T>(element); } } else { int size = array.Length; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); } }

我只想检查array的元素是否是数组,如果是,我再次为array每个element调用函数Print。 但Print(element)不起作用,因为element是T类型而不是T[] ,我不知道如何告诉编译器在这种情况下T是一个数组。 我需要做些什么来完成这项工作?

I am currently working with lots of arrays and for debugging purposes I wrote a generic Print() method to print different kinds of arrays

static void Main() { Print(new double[]{ 1, 2, 3 }); // Output: [ 1, 2, 3 ] } static void Print<T>(T[] array) { int size = array.Length; if (size == 0) return; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); }

which works fine so far. Then I wanted to print an array of arrays, like double[][], and tried the following:

static void Main() { Print(new double[][] { new double[] { 1, 2 }, new double[] { 3, 4 }, new double[] { 5, 6 }, }); // Output: [ 1, 2 ] // [ 3, 4 ] // [ 5, 6 ] } static void Print<T>(T[] array) { if (array.Length == 0) return; if (array[0].GetType().IsArray) { foreach (var element in array) { Print<T>(element); } } else { int size = array.Length; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); } }

I just wanted to check if the elements of array again are arrays, and if so, I call the function Print again for each element of array. But Print(element) doesn't work, since element is of type T and not T[] and I don't know how to tell the compiler that in this case T is an array. What do I have to do to make this work?

最满意答案

你可以用动力学做到这一点:

void Main() { Print(new double[][] { new double[] { 1, 2 }, new double[] { 3, 4 }, new double[] { 5, 6 }, }); // Output: [ 1, 2 ] // [ 3, 4 ] // [ 5, 6 ] } static void Print(dynamic array) { if (array.Length == 0) return; if (array[0].GetType().IsArray) { foreach (var element in array) { Print(element); } } else { int size = array.Length; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); } }

如果您只想测试较小部分的代码 - 我建议使用LinqPad,你有AnyType.Dump()方法,你不能实现任何东西;)

You can do it using dynamics:

void Main() { Print(new double[][] { new double[] { 1, 2 }, new double[] { 3, 4 }, new double[] { 5, 6 }, }); // Output: [ 1, 2 ] // [ 3, 4 ] // [ 5, 6 ] } static void Print(dynamic array) { if (array.Length == 0) return; if (array[0].GetType().IsArray) { foreach (var element in array) { Print(element); } } else { int size = array.Length; string str = "[ "; for (int i = 0; i < size; i++) { str += array[i].ToString(); if (i < size - 1) str += ", "; } str += " ]"; Console.WriteLine(str); } }

If you only want to test smaller part of codes - I suggest using LinqPad where you have AnyType.Dump() method and you mustn't implement anything ;)

更多推荐

本文发布于:2023-04-29 01:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:锯齿状   阵列   Generic   arrays   jagged

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!