C# c中每个元素上两个数组比较的最小值#
下面是第1个元素的代码…我只是不知道如何对数组中的所有元素执行此操作。。。提前谢谢C# c中每个元素上两个数组比较的最小值#,c#,C#,下面是第1个元素的代码…我只是不知道如何对数组中的所有元素执行此操作。。。提前谢谢 int[] ListNumb1 = new int[] { 2, 4, 6 }; int[] ListNumb2 = new int[] { 3, 1, 9 }; if (ListNumb1[0] < ListNumb2[0]) { Console.WriteLine(ListNumb1[0]); }
int[] ListNumb1 = new int[] { 2, 4, 6 };
int[] ListNumb2 = new int[] { 3, 1, 9 };
if (ListNumb1[0] < ListNumb2[0])
{
Console.WriteLine(ListNumb1[0]);
}
else
Console.WriteLine(ListNumb2[0]);
Console.ReadLine();
int[]ListNumb1=新的int[]{2,4,6};
int[]ListNumb2=新的int[]{3,1,9};
if(ListNumb1[0]
您可以使用该方法将两个集合(数组)压缩在一起,并获得最低值
var result = ListNumb1.Zip(ListNumb2, Math.Min)
完整示例
int[] ListNumb1 = new int[] { 2, 4, 6 };
int[] ListNumb2 = new int[] { 3, 1, 9 };
// Result will be an IEnumerbale<int>
var result = ListNumb1.Zip(ListNumb2, Math.Min)
Console.WriteLine(string.Join(",", result));
注意:要获取数组中的输出,只需调用ListNumb1.Zip(ListNumb2,Math.Min).ToArray()
或者你可以用经典的for循环
//分配数组
var结果=新整数[ListNumb1.Length];
//迭代两个数组中的每个元素
for(var i=0;i
注2:这两个示例都假设数组长度相等,如果不是这样,则需要进行验证并采取相应的行动
其他资源
- 将指定的函数应用于两个 序列,生成结果序列
- 返回两个数字中较小的一个
- 连接指定数组的元素或 集合,在每个元素或元素之间使用指定的分隔符 成员
2,1,6
// Allocate the array
var results = new int[ListNumb1.Length];
// Iterate over each element in both arrays
for (var i = 0; i < ListNumb1.Length; i++)
results[i] = Math.Min(ListNumb1[i], ListNumb2[i]);