【发布时间】:2021-02-05 13:23:07
【问题描述】:
我有一个功能:
public static bool Append<T>(this List<T> list, T value)
where T : IComparable<T>
{
int l = 0;
int r = list.Count() - 1;
int c = 0;
var com = 0;
while (l <= r)
{
c = (l + r) / 2;
com = list[c].CompareTo(value);
if (com > 0) r = c - 1;
else if (com < 0) l = c + 1;
else
{
list[c] = value;
return false;
}
}
if (c >= list.Count())
list.Add(value);
else
{
if (com < 0) c++;
list.Insert(c, value);
}
return true;
}
我想在 java 中重新创建它,但是有没有与
where T: IComparable<T> 在 java 中?
public static <T> void append(List<T> list)
{
}
我知道 java 有接口 java.lang.Comparable 但我不知道如何在函数中实现它
【问题讨论】:
-
是的,但由于某种原因,函数不能是静态的,我将把这个问题标记为重复
-
呃,你应该可以用静态方法做到这一点。你能把你写的东西展示一下吗?
-
@FedericoklezCulloca 我写错了
public <T extends Comparable<T>> static int append(List<T> list, T value)而不是public static <T extends Comparable<T>> int append(List<T> list, T value) -
你能删除关于这个问题的重复注释吗?