static void Main(string[] args)
{
int[] a=new int[]{1,23,4,4,1,2,3,4,5,6,5,4,3,2,1,3,4}; Console.WriteLine("排序前数组");
for (int j = 0; j < a.Length; j++)
{
Console.WriteLine("{0} ", a[j]); } Sort.QuickSort0926<int>(a, 0, a.Length - 1); Console.WriteLine("排序后数组");
for (int j = 0; j < a.Length; j++)
{
Console.WriteLine("{0} ", a[j]); }
Console.ReadLine();
} public static void QuickSort0926<T>(T[] data,int low,int high)
{
if (low > high)
return; //分割
int l = low;
int h = high;
//取出比较值
T pivot=data[low];
while(l<h)
{ while (l<h && data[h] >= pivot)
{
h--;
}
if (l < h)
{
data[l] = data[h];
l++;
}
while (l < h && data[l] <= pivot)
{
l++;
}
if (l < h)
{
data[h] = data[l];
h--;
}
}
data[l] = pivot; //递归
QuickSort0926(data, low, l - 1);
QuickSort0926(data, l + 1, high); }编译时会报错,提示: 错误 1 运算符“>=”无法应用于“T”和“T”类型的操作数
错误 2 运算符“<=”无法应用于“T”和“T”类型的操作数请问我这中泛型用法有问题吗?
{
int[] a=new int[]{1,23,4,4,1,2,3,4,5,6,5,4,3,2,1,3,4}; Console.WriteLine("排序前数组");
for (int j = 0; j < a.Length; j++)
{
Console.WriteLine("{0} ", a[j]); } Sort.QuickSort0926<int>(a, 0, a.Length - 1); Console.WriteLine("排序后数组");
for (int j = 0; j < a.Length; j++)
{
Console.WriteLine("{0} ", a[j]); }
Console.ReadLine();
} public static void QuickSort0926<T>(T[] data,int low,int high)
{
if (low > high)
return; //分割
int l = low;
int h = high;
//取出比较值
T pivot=data[low];
while(l<h)
{ while (l<h && data[h] >= pivot)
{
h--;
}
if (l < h)
{
data[l] = data[h];
l++;
}
while (l < h && data[l] <= pivot)
{
l++;
}
if (l < h)
{
data[h] = data[l];
h--;
}
}
data[l] = pivot; //递归
QuickSort0926(data, low, l - 1);
QuickSort0926(data, l + 1, high); }编译时会报错,提示: 错误 1 运算符“>=”无法应用于“T”和“T”类型的操作数
错误 2 运算符“<=”无法应用于“T”和“T”类型的操作数请问我这中泛型用法有问题吗?
public static void QuickSort0926<T>(T[] data,int low,int high)
这那里是范性呀,明明就是名字叫QuickSort0926<T>的方法,方法的名字中是不能带<>符号的.好好再看看范型的定义吧..
这才叫泛型 方法名都不会写<>这种符号都拿出来 不错才怪
我猜,List<>的原型应该是class List<T>{}
说话那么冲!
即使这样你把QuickSort0926独立成泛型类中的方法一样也过不了编译!唉!没人知道吗?