using System;
using System.Collections.Generic;
using System.Text; namespace ConsoleApp
{
class Program
{
static void Main()
{
int[] tarr = {12,34,45,23,11,14,15,56,87,56,90};
Console.WriteLine(testapp.SelectSort(tarr));
}
} class testapp
{
public static int[] SelectSort(int[] arr)
{
int i, j, k;
for (i = 1; i < arr.Length; i++)
{
k = i;
for (j = i + 1; j < arr.Length; j++)
{
if (arr[j]< arr[k])
k = j;
if (k != i)
{
arr[0] = arr[i];
arr[i] = arr[k];
arr[k] = arr[0];
}
}
}
return arr;
}
} }为什么返回的是System.int32[]呢?怎么修改可以得到我想要的排序结果?
using System.Collections.Generic;
using System.Text; namespace ConsoleApp
{
class Program
{
static void Main()
{
int[] tarr = {12,34,45,23,11,14,15,56,87,56,90};
Console.WriteLine(testapp.SelectSort(tarr));
}
} class testapp
{
public static int[] SelectSort(int[] arr)
{
int i, j, k;
for (i = 1; i < arr.Length; i++)
{
k = i;
for (j = i + 1; j < arr.Length; j++)
{
if (arr[j]< arr[k])
k = j;
if (k != i)
{
arr[0] = arr[i];
arr[i] = arr[k];
arr[k] = arr[0];
}
}
}
return arr;
}
} }为什么返回的是System.int32[]呢?怎么修改可以得到我想要的排序结果?
int本来就是System.Int32的别名.2. 如果要排序,Array类有一个静态方法Sort()可以对一维数组进行排序
public static void Sort(Array);
int[] tarr1 = testapp.SelectSort(tarr);
for(int i=0;i<tarr1.Length;i++)
{
Console.WriteLine(tarr1[i]);
}
你的testapp类中的静态方法SelectSort的返回值类型为int[],这就是一般整型的数组,对应到CTS就是System.Int32[]。Console.WriteLine()方法中的参数可以是字符型、数字型和字符串型等等,但是不能直接输出整个数组或集合中的所有元素,所以我们可以遍历返回的数组中的所有元素,一个一个的输出:
int[] array=testapp.SelectSort(tarr);
foreach(int i in array)
{
Console.WriteLine(i);
}