{int a;
T temp;
Node temp1;
Node temp2;
for (temp1 = head; temp1.Next != null; temp1 = temp1.Next)
for (temp2 = temp1.Next; temp2 != null; temp2 = temp2.Next)
if (object.Equals(temp1.Data, temp2.Data).CompareTo(a))//这里出错
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}
}
temp1.data,temp2.data为泛型的链表值,现在要进行排序,比较大小用object.Equals(),那object.Equals().CompareTo()这个的使用方法不懂,,有谁能告诉我一下吗??
16 个解决方案
#1
沙发
#2
在线等....
#3
代码要贴也贴全一点,Node怎么声明的?最好给出可调试的代码。。。
我猜估计该是这样:
我猜估计该是这样:
if (temp1 is IComparable) // 判断是否具有IComparable接口,以确定是否存在CompareTo()方法
{
if ((temp1 as IComparable).CompareTo(temp2) > 0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}
}
#4
楼主的错误是object.Equals()返回一个bool类型
object.Equals().CompareTo(a)
成了bool.CompareTo(a),就是bool类型间的比较。
object.Equals().CompareTo(a)
成了bool.CompareTo(a),就是bool类型间的比较。
#5
类一
using System;
using System.Collections.Generic;
public class MyList<T>
{
private Node head;
private Node tail;
private class Node
{
private Node next;
private T data;
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
public T Data
{
get { return data; }
set { data = value; }
}
}
public MyList()
{
head = null;
tail = null;
}
public void AddHead(T t)//增加
{
Node n = new Node(t);
n.Next = head;
head = n;
}
public void Delete(T t)//删除
{
Node temp = head;
Node n = new Node(t);
while (!object.Equals(n.Data, temp.Data))
{
tail = temp;
temp = temp.Next;
}
if (object.Equals(n.Data, temp.Data))
{
if (object.Equals(head.Data, n.Data))
{
head = temp.Next;
}
else
{
tail.Next = temp.Next;
}
}
}
public void Revise(T t,T r)
{
Node temp = head;
Node n = new Node(t);
Node revise = new Node(r);
while (!object.Equals(n.Data, temp.Data))
{
tail = temp;
temp = temp.Next;
}
if (object.Equals(n.Data, temp.Data))
{
if (object.Equals(n.Data, head.Data))
{
head.Data = revise.Data;
}
else
{
temp.Data = revise.Data;
}
}
}
public void sort()
{
T temp;
Node temp1;
Node temp2;
for (temp1 = head; temp1.Next != null; temp1 = temp1.Next)
for (temp2 = temp1.Next; temp2 != null; temp2 = temp2.Next)
{
if (temp1 is IComparable) // 判断是否具有IComparable接口,以确定是否存在CompareTo()方法
{
if ((temp1 as IComparable).CompareTo(temp2) > 0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}
}
/*
if (comparer.Compare(temp1.Data, temp2.Data)>0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}*/
}
}
/* if (head == null || head.Next == null)
return;
bool swapped;
do
{
Node previous = null;
Node current = head;
swapped = false;
while (current.next != null)
{
if (current.DataCompareTo(current.next.Data) > 0)
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current;
if (previous == null)
{
Node1 = tmp;
}
else
{
previous.next = tmp;
}
previous = tmp;
swapped = true;
}
else
{
previous = current;
current = current.next;
}
}
}
while (swapped);
}*/
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
using System;
using System.Collections.Generic;
public class MyList<T>
{
private Node head;
private Node tail;
private class Node
{
private Node next;
private T data;
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
public T Data
{
get { return data; }
set { data = value; }
}
}
public MyList()
{
head = null;
tail = null;
}
public void AddHead(T t)//增加
{
Node n = new Node(t);
n.Next = head;
head = n;
}
public void Delete(T t)//删除
{
Node temp = head;
Node n = new Node(t);
while (!object.Equals(n.Data, temp.Data))
{
tail = temp;
temp = temp.Next;
}
if (object.Equals(n.Data, temp.Data))
{
if (object.Equals(head.Data, n.Data))
{
head = temp.Next;
}
else
{
tail.Next = temp.Next;
}
}
}
public void Revise(T t,T r)
{
Node temp = head;
Node n = new Node(t);
Node revise = new Node(r);
while (!object.Equals(n.Data, temp.Data))
{
tail = temp;
temp = temp.Next;
}
if (object.Equals(n.Data, temp.Data))
{
if (object.Equals(n.Data, head.Data))
{
head.Data = revise.Data;
}
else
{
temp.Data = revise.Data;
}
}
}
public void sort()
{
T temp;
Node temp1;
Node temp2;
for (temp1 = head; temp1.Next != null; temp1 = temp1.Next)
for (temp2 = temp1.Next; temp2 != null; temp2 = temp2.Next)
{
if (temp1 is IComparable) // 判断是否具有IComparable接口,以确定是否存在CompareTo()方法
{
if ((temp1 as IComparable).CompareTo(temp2) > 0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}
}
/*
if (comparer.Compare(temp1.Data, temp2.Data)>0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}*/
}
}
/* if (head == null || head.Next == null)
return;
bool swapped;
do
{
Node previous = null;
Node current = head;
swapped = false;
while (current.next != null)
{
if (current.DataCompareTo(current.next.Data) > 0)
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current;
if (previous == null)
{
Node1 = tmp;
}
else
{
previous.next = tmp;
}
previous = tmp;
swapped = true;
}
else
{
previous = current;
current = current.next;
}
}
}
while (swapped);
}*/
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
#6
program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i;
MyList<int> list = new MyList<int>();
Console.WriteLine(" 选择功能 ");
Console.WriteLine("1.创建新链表");
Console.WriteLine("2.显示所有链表元素");
Console.WriteLine("3.删除元素");
Console.WriteLine("4.修改元素");
Console.WriteLine("5.排序");
Console.WriteLine("6.退出");
Console.Write("请选择功能:");
i = int.Parse(Console.ReadLine());
while (i != 6)
{
switch (i)
{
case 1:
int x;
Console.WriteLine("x=");
bool t = true;
while (t)
{
x = int.Parse(Console.ReadLine());
if (x == 0)
{ t = false; }
else
{
list.AddHead(x);
}
}
break;
case 2:
foreach (int k in list)
{
Console.Write(k + " ");
}
Console.WriteLine();
break;
case 3:
Console.WriteLine("请输入要删除的数据c=");//删除
int c = int.Parse(Console.ReadLine());
list.Delete(c);
break;
case 4:
Console.WriteLine("请输入要修改的数据n=");//修改
int n = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改好的数据r=");
int r = int.Parse(Console.ReadLine());
list.Revise(n, r);
break;
case 5: list.sort();
break;
default: Console.WriteLine("请输入正确的数字");
break;
}
Console.Write("重新选择你想要的功能:");
i = int.Parse(Console.ReadLine());
}
/*int x;
Console.WriteLine("x=");
do
{
x = int.Parse(Console.ReadLine());
list.AddHead(x);
} while (x != 0) ;
foreach (int i in list)
{
Console.Write(i+" ");
}
Console.WriteLine("Done,请输入要删除的数据c=");//删除
int c = int.Parse(Console.ReadLine());
list.Delete(c);
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine("Done");
Console.WriteLine("请输入要修改的数据n=");//修改
int n = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改好的数据r=");
int r = int.Parse(Console.ReadLine());
list.Revise(n,r);
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine("Done");
Console.ReadKey();*/
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i;
MyList<int> list = new MyList<int>();
Console.WriteLine(" 选择功能 ");
Console.WriteLine("1.创建新链表");
Console.WriteLine("2.显示所有链表元素");
Console.WriteLine("3.删除元素");
Console.WriteLine("4.修改元素");
Console.WriteLine("5.排序");
Console.WriteLine("6.退出");
Console.Write("请选择功能:");
i = int.Parse(Console.ReadLine());
while (i != 6)
{
switch (i)
{
case 1:
int x;
Console.WriteLine("x=");
bool t = true;
while (t)
{
x = int.Parse(Console.ReadLine());
if (x == 0)
{ t = false; }
else
{
list.AddHead(x);
}
}
break;
case 2:
foreach (int k in list)
{
Console.Write(k + " ");
}
Console.WriteLine();
break;
case 3:
Console.WriteLine("请输入要删除的数据c=");//删除
int c = int.Parse(Console.ReadLine());
list.Delete(c);
break;
case 4:
Console.WriteLine("请输入要修改的数据n=");//修改
int n = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改好的数据r=");
int r = int.Parse(Console.ReadLine());
list.Revise(n, r);
break;
case 5: list.sort();
break;
default: Console.WriteLine("请输入正确的数字");
break;
}
Console.Write("重新选择你想要的功能:");
i = int.Parse(Console.ReadLine());
}
/*int x;
Console.WriteLine("x=");
do
{
x = int.Parse(Console.ReadLine());
list.AddHead(x);
} while (x != 0) ;
foreach (int i in list)
{
Console.Write(i+" ");
}
Console.WriteLine("Done,请输入要删除的数据c=");//删除
int c = int.Parse(Console.ReadLine());
list.Delete(c);
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine("Done");
Console.WriteLine("请输入要修改的数据n=");//修改
int n = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改好的数据r=");
int r = int.Parse(Console.ReadLine());
list.Revise(n,r);
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine("Done");
Console.ReadKey();*/
}
}
}
#7
嘿嘿,这就是所有的代码啦...
直接建两个类再全COPY进去就可以动行啦..
其他功能都行,就是排序那里搞不了,,哪位大哥有空帮我看看,不会花很多时间的...
直接建两个类再全COPY进去就可以动行啦..
其他功能都行,就是排序那里搞不了,,哪位大哥有空帮我看看,不会花很多时间的...
#8
怎么加个IComparable接口?刚刚学习泛型,很多不懂,呵呵,,
#9
object.Equals(temp1.Data, temp2.Data).CompareTo(a))
这段代码莫明其妙啊?object.Equals应该返回一个bool值,bool类型有CompareTo方法吗?不错才怪。
这段代码莫明其妙啊?object.Equals应该返回一个bool值,bool类型有CompareTo方法吗?不错才怪。
#10
那个是乱写的..
现在的问题就是如何加个IComparable的接口....
现在的问题就是如何加个IComparable的接口....
#11
在线等啊...
#12
实现 IComparable 接口只需实现一个 CompareTo() 方法就行了,以便为你的自定义类型提供比较大小的方法。
个人认为应该在声明泛型时指明约束条件,比如:
这样,T 就被限制为实现了 IComparable<T> 接口的类。
当然如果你愿意,也可以使用 IComparable 而非其泛型版本,IComparable 在泛型概念引入之前就存在于 .NET 类库了。
上面的做法会使编译器检查实例化该泛型时的类型是否满足 where 后面的条件,即该类型是否实现了 IComparable<T> 接口,如果不符则会报错。
个人认为应该在声明泛型时指明约束条件,比如:
public class ComparableList<T> : where T : IComparable<T>
{
...
这样,T 就被限制为实现了 IComparable<T> 接口的类。
当然如果你愿意,也可以使用 IComparable 而非其泛型版本,IComparable 在泛型概念引入之前就存在于 .NET 类库了。
上面的做法会使编译器检查实例化该泛型时的类型是否满足 where 后面的条件,即该类型是否实现了 IComparable<T> 接口,如果不符则会报错。
#13
LaoBai,你怎么现在才来啊....我刚刚突破了,,终于搞定了,就是你这样子啊....想到我脑痛啊,,第一次看接口泛型啊...多谢楼上各位的帮忙了..小子结贴了..
#14
佩服楼主的无满意答案结贴
#15
BS一下
#16
honey52570,无满意结贴嘛,第一是我没有分可以给大家,第二是我自己想出来做好了才看到那位大哥的建议.
#1
沙发
#2
在线等....
#3
代码要贴也贴全一点,Node怎么声明的?最好给出可调试的代码。。。
我猜估计该是这样:
我猜估计该是这样:
if (temp1 is IComparable) // 判断是否具有IComparable接口,以确定是否存在CompareTo()方法
{
if ((temp1 as IComparable).CompareTo(temp2) > 0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}
}
#4
楼主的错误是object.Equals()返回一个bool类型
object.Equals().CompareTo(a)
成了bool.CompareTo(a),就是bool类型间的比较。
object.Equals().CompareTo(a)
成了bool.CompareTo(a),就是bool类型间的比较。
#5
类一
using System;
using System.Collections.Generic;
public class MyList<T>
{
private Node head;
private Node tail;
private class Node
{
private Node next;
private T data;
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
public T Data
{
get { return data; }
set { data = value; }
}
}
public MyList()
{
head = null;
tail = null;
}
public void AddHead(T t)//增加
{
Node n = new Node(t);
n.Next = head;
head = n;
}
public void Delete(T t)//删除
{
Node temp = head;
Node n = new Node(t);
while (!object.Equals(n.Data, temp.Data))
{
tail = temp;
temp = temp.Next;
}
if (object.Equals(n.Data, temp.Data))
{
if (object.Equals(head.Data, n.Data))
{
head = temp.Next;
}
else
{
tail.Next = temp.Next;
}
}
}
public void Revise(T t,T r)
{
Node temp = head;
Node n = new Node(t);
Node revise = new Node(r);
while (!object.Equals(n.Data, temp.Data))
{
tail = temp;
temp = temp.Next;
}
if (object.Equals(n.Data, temp.Data))
{
if (object.Equals(n.Data, head.Data))
{
head.Data = revise.Data;
}
else
{
temp.Data = revise.Data;
}
}
}
public void sort()
{
T temp;
Node temp1;
Node temp2;
for (temp1 = head; temp1.Next != null; temp1 = temp1.Next)
for (temp2 = temp1.Next; temp2 != null; temp2 = temp2.Next)
{
if (temp1 is IComparable) // 判断是否具有IComparable接口,以确定是否存在CompareTo()方法
{
if ((temp1 as IComparable).CompareTo(temp2) > 0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}
}
/*
if (comparer.Compare(temp1.Data, temp2.Data)>0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}*/
}
}
/* if (head == null || head.Next == null)
return;
bool swapped;
do
{
Node previous = null;
Node current = head;
swapped = false;
while (current.next != null)
{
if (current.DataCompareTo(current.next.Data) > 0)
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current;
if (previous == null)
{
Node1 = tmp;
}
else
{
previous.next = tmp;
}
previous = tmp;
swapped = true;
}
else
{
previous = current;
current = current.next;
}
}
}
while (swapped);
}*/
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
using System;
using System.Collections.Generic;
public class MyList<T>
{
private Node head;
private Node tail;
private class Node
{
private Node next;
private T data;
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
public T Data
{
get { return data; }
set { data = value; }
}
}
public MyList()
{
head = null;
tail = null;
}
public void AddHead(T t)//增加
{
Node n = new Node(t);
n.Next = head;
head = n;
}
public void Delete(T t)//删除
{
Node temp = head;
Node n = new Node(t);
while (!object.Equals(n.Data, temp.Data))
{
tail = temp;
temp = temp.Next;
}
if (object.Equals(n.Data, temp.Data))
{
if (object.Equals(head.Data, n.Data))
{
head = temp.Next;
}
else
{
tail.Next = temp.Next;
}
}
}
public void Revise(T t,T r)
{
Node temp = head;
Node n = new Node(t);
Node revise = new Node(r);
while (!object.Equals(n.Data, temp.Data))
{
tail = temp;
temp = temp.Next;
}
if (object.Equals(n.Data, temp.Data))
{
if (object.Equals(n.Data, head.Data))
{
head.Data = revise.Data;
}
else
{
temp.Data = revise.Data;
}
}
}
public void sort()
{
T temp;
Node temp1;
Node temp2;
for (temp1 = head; temp1.Next != null; temp1 = temp1.Next)
for (temp2 = temp1.Next; temp2 != null; temp2 = temp2.Next)
{
if (temp1 is IComparable) // 判断是否具有IComparable接口,以确定是否存在CompareTo()方法
{
if ((temp1 as IComparable).CompareTo(temp2) > 0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}
}
/*
if (comparer.Compare(temp1.Data, temp2.Data)>0)
{
temp = temp1.Data;
temp1.Data = temp2.Data;
temp2.Data = temp;
}*/
}
}
/* if (head == null || head.Next == null)
return;
bool swapped;
do
{
Node previous = null;
Node current = head;
swapped = false;
while (current.next != null)
{
if (current.DataCompareTo(current.next.Data) > 0)
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current;
if (previous == null)
{
Node1 = tmp;
}
else
{
previous.next = tmp;
}
previous = tmp;
swapped = true;
}
else
{
previous = current;
current = current.next;
}
}
}
while (swapped);
}*/
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
#6
program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i;
MyList<int> list = new MyList<int>();
Console.WriteLine(" 选择功能 ");
Console.WriteLine("1.创建新链表");
Console.WriteLine("2.显示所有链表元素");
Console.WriteLine("3.删除元素");
Console.WriteLine("4.修改元素");
Console.WriteLine("5.排序");
Console.WriteLine("6.退出");
Console.Write("请选择功能:");
i = int.Parse(Console.ReadLine());
while (i != 6)
{
switch (i)
{
case 1:
int x;
Console.WriteLine("x=");
bool t = true;
while (t)
{
x = int.Parse(Console.ReadLine());
if (x == 0)
{ t = false; }
else
{
list.AddHead(x);
}
}
break;
case 2:
foreach (int k in list)
{
Console.Write(k + " ");
}
Console.WriteLine();
break;
case 3:
Console.WriteLine("请输入要删除的数据c=");//删除
int c = int.Parse(Console.ReadLine());
list.Delete(c);
break;
case 4:
Console.WriteLine("请输入要修改的数据n=");//修改
int n = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改好的数据r=");
int r = int.Parse(Console.ReadLine());
list.Revise(n, r);
break;
case 5: list.sort();
break;
default: Console.WriteLine("请输入正确的数字");
break;
}
Console.Write("重新选择你想要的功能:");
i = int.Parse(Console.ReadLine());
}
/*int x;
Console.WriteLine("x=");
do
{
x = int.Parse(Console.ReadLine());
list.AddHead(x);
} while (x != 0) ;
foreach (int i in list)
{
Console.Write(i+" ");
}
Console.WriteLine("Done,请输入要删除的数据c=");//删除
int c = int.Parse(Console.ReadLine());
list.Delete(c);
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine("Done");
Console.WriteLine("请输入要修改的数据n=");//修改
int n = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改好的数据r=");
int r = int.Parse(Console.ReadLine());
list.Revise(n,r);
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine("Done");
Console.ReadKey();*/
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i;
MyList<int> list = new MyList<int>();
Console.WriteLine(" 选择功能 ");
Console.WriteLine("1.创建新链表");
Console.WriteLine("2.显示所有链表元素");
Console.WriteLine("3.删除元素");
Console.WriteLine("4.修改元素");
Console.WriteLine("5.排序");
Console.WriteLine("6.退出");
Console.Write("请选择功能:");
i = int.Parse(Console.ReadLine());
while (i != 6)
{
switch (i)
{
case 1:
int x;
Console.WriteLine("x=");
bool t = true;
while (t)
{
x = int.Parse(Console.ReadLine());
if (x == 0)
{ t = false; }
else
{
list.AddHead(x);
}
}
break;
case 2:
foreach (int k in list)
{
Console.Write(k + " ");
}
Console.WriteLine();
break;
case 3:
Console.WriteLine("请输入要删除的数据c=");//删除
int c = int.Parse(Console.ReadLine());
list.Delete(c);
break;
case 4:
Console.WriteLine("请输入要修改的数据n=");//修改
int n = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改好的数据r=");
int r = int.Parse(Console.ReadLine());
list.Revise(n, r);
break;
case 5: list.sort();
break;
default: Console.WriteLine("请输入正确的数字");
break;
}
Console.Write("重新选择你想要的功能:");
i = int.Parse(Console.ReadLine());
}
/*int x;
Console.WriteLine("x=");
do
{
x = int.Parse(Console.ReadLine());
list.AddHead(x);
} while (x != 0) ;
foreach (int i in list)
{
Console.Write(i+" ");
}
Console.WriteLine("Done,请输入要删除的数据c=");//删除
int c = int.Parse(Console.ReadLine());
list.Delete(c);
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine("Done");
Console.WriteLine("请输入要修改的数据n=");//修改
int n = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改好的数据r=");
int r = int.Parse(Console.ReadLine());
list.Revise(n,r);
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine("Done");
Console.ReadKey();*/
}
}
}
#7
嘿嘿,这就是所有的代码啦...
直接建两个类再全COPY进去就可以动行啦..
其他功能都行,就是排序那里搞不了,,哪位大哥有空帮我看看,不会花很多时间的...
直接建两个类再全COPY进去就可以动行啦..
其他功能都行,就是排序那里搞不了,,哪位大哥有空帮我看看,不会花很多时间的...
#8
怎么加个IComparable接口?刚刚学习泛型,很多不懂,呵呵,,
#9
object.Equals(temp1.Data, temp2.Data).CompareTo(a))
这段代码莫明其妙啊?object.Equals应该返回一个bool值,bool类型有CompareTo方法吗?不错才怪。
这段代码莫明其妙啊?object.Equals应该返回一个bool值,bool类型有CompareTo方法吗?不错才怪。
#10
那个是乱写的..
现在的问题就是如何加个IComparable的接口....
现在的问题就是如何加个IComparable的接口....
#11
在线等啊...
#12
实现 IComparable 接口只需实现一个 CompareTo() 方法就行了,以便为你的自定义类型提供比较大小的方法。
个人认为应该在声明泛型时指明约束条件,比如:
这样,T 就被限制为实现了 IComparable<T> 接口的类。
当然如果你愿意,也可以使用 IComparable 而非其泛型版本,IComparable 在泛型概念引入之前就存在于 .NET 类库了。
上面的做法会使编译器检查实例化该泛型时的类型是否满足 where 后面的条件,即该类型是否实现了 IComparable<T> 接口,如果不符则会报错。
个人认为应该在声明泛型时指明约束条件,比如:
public class ComparableList<T> : where T : IComparable<T>
{
...
这样,T 就被限制为实现了 IComparable<T> 接口的类。
当然如果你愿意,也可以使用 IComparable 而非其泛型版本,IComparable 在泛型概念引入之前就存在于 .NET 类库了。
上面的做法会使编译器检查实例化该泛型时的类型是否满足 where 后面的条件,即该类型是否实现了 IComparable<T> 接口,如果不符则会报错。
#13
LaoBai,你怎么现在才来啊....我刚刚突破了,,终于搞定了,就是你这样子啊....想到我脑痛啊,,第一次看接口泛型啊...多谢楼上各位的帮忙了..小子结贴了..
#14
佩服楼主的无满意答案结贴
#15
BS一下
#16
honey52570,无满意结贴嘛,第一是我没有分可以给大家,第二是我自己想出来做好了才看到那位大哥的建议.