我正在编写一个代码,其中包含以下几行:
public static int[] toArray(List<Integer> list) {
int[] array = new int[list.size()];
for(int i = 0; i < array.length; i++)
array[i] = list.get(i);
return array;
}
public static double[] toArray(List<Double> list) {
double[] array = new double[list.size()];
for(int i = 0; i < array.length; i++)
array[i] = list.get(i);
return array;
}
public static String[] toArray(List<String> list) {
String[] data = new String[list.size()];
for(int i = 0; i < data.length; i++)
data[i] = list.get(i);
return data;
编译时出现以下错误:
error: name clash: toArray(List<Double>) and toArray(List<Integer>) have the same erasure
public static double[] toArray(List<Double> list) {
^
error: name clash: toArray(List<String>) and toArray(List<Integer>) have the same erasure
public static String[] toArray(List<String> list) {
^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
这个程序是我最近下载的一个开源程序(有点旧,2006 年)。在包中,我可以找到二进制版本和源代码。这意味着这段代码已经正确编译,但现在我无法编译它。
现在我有两个问题:
- 这种重载有什么问题?
- 我该如何解决这个问题?我可以用基于模板的函数替换这些函数吗?如果是这样,怎么做?
谢谢