【发布时间】:2020-10-27 03:26:32
【问题描述】:
我想编写一个函数,给定输入中的整数数组,输出中返回整数数组,其中包含每个内部数组的元素数。
这是我当前的实现:
public static int[] countEach(int[][] a) {
int[] count = new int[3];
for(int i = 0; i < a.Length; i++){
count[i] = a[i].Length;
}
return count;
}
public static void Main(string[] args){
int[][] a = new int[][]{
new int[] {1, 2, 3},
new int[] {1, 2, 3, 4, 5},
new int[] {1}
};
int[] result = countEach(a);
}
它可以工作,但是我不想事先定义一个固定长度为 3。那么我该如何重写它以便它可以接受任何输入数组呢?我想不出,有没有更好的方法来编码?这样我可以更好地掌握c#的编程概念。谢谢
【问题讨论】:
标签: c# arrays loops for-loop element