我想通过使用ArrayList创建自己的多线程mergesort算法。我在Java中熟悉这种方法,但试图将它带入C#并没有按计划运行。尝试比较两个ArrayList项目Error 1 Operator '<' cannot be applied to operands of type 'object' and 'object'
时出现以下错误。我知道你不能直接比较这样的两个对象,在Java中你可以使用compareTo或类似的东西,是否有任何等效的C#?检查ArrayList中的哪个项更小c#
这里是导致错误的代码,如果你需要的话,记住我从我的一个Java程序中使用整型数组复制了它。
int size = (last - first) + 1;
ArrayList temp = new ArrayList();
int mid = (first + last)/2;
int i1 = 0;
int i2 = first;
int i3 = mid + 1;
while(i2 <= mid && i3 <= last)
{
if(list[i2] < list[i3])
temp[i1++] = list[i2++];
else temp[i1++] = list[i3++];
}
while(i2 <= mid)
temp[i1++] = list[i2++];
while(i3 <= last)
temp[i1++] = list[i3++];
i3 = first;
for(i1 = 0; i1 < temp.Count; i1++, i3++)
list[i3] = temp[i1];
这里有一个提示:不要使用'ArrayList',使用类型列表,例如'List'。 –
DavidG
你的'list' var声明在哪里? –
好的,我将如何去比较列表中的两个项目? –