定义一个数组,将数组作为参数通过方法求出数组的最大值(ref)最小值(out)和排序后的数组(return)
时间: 2024-09-14 18:14:38 浏览: 65
在C#中,你可以创建一个方法接受一个整数数组作为输入,并返回最大值、最小值以及排序后的数组。这里是一个示例:
```csharp
using System;
using System.Linq; // 需要这个命名空间来进行排序操作
public class ArrayHelper
{
public static int[] FindMinMaxSort(int[] inputArray)
{
if (inputArray == null || inputArray.Length == 0) return new int[3]; // 返回空数组表示无效输入
// 找到最大值(ref参数)
int max = inputArray[0];
for (int i = 1; i < inputArray.Length; i++)
{
if (inputArray[i] > max)
{
max = inputArray[i];
}
}
// 最小值(out参数,需要预先声明)
int min;
min = int.MaxValue; // 初始假设所有元素都很大
for (int i = 0; i < inputArray.Length; i++)
{
if (inputArray[i] < min)
{
min = inputArray[i];
}
}
// 排序数组并返回(新的实例)
Array.Sort(inputArray);
return inputArray.Concat(new[] { max, min }).ToArray();
}
}
// 调用方法示例
public static void Main()
{
int[] array = { 5, 9, 1, 7, 3 };
int[] result = ArrayHelper.FindMinMaxSort(array);
Console.WriteLine($"最大值: {result[0]}, 最小值: {result[1]}, 排序后数组: {string.Join(", ", result)}");
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)