我不是第一次遇到这种“奇怪”的情况了。我需要为我的类创建两个不同的构造函数:
public OpponentListAdapter(Context c, ArrayList<MyCustomObject> l){}
和
public OpponentListAdapter(Context c, ArrayList<String> l){}
因为根据 ArrayList 泛型的类型,我需要执行不同的操作。 但我有这个错误:
Method OpponentListAdapter(Context, ArrayList) has the same erasure >OpponentListAdapter(Context, ArrayList) as another method in type OpponentListAdapter
怎么了? 也许解决方案很简单,但目前我找不到什么好的解决方案!
最佳答案
两者都是ArrayList<String>
和ArrayList<MyCustomObject>
具有相同的删除ArrayList
。因此,两个构造函数在运行时将具有相同的签名,因此会出现异常,因为那里有一个重复的构造函数。
如果您希望构造函数采用不同类型的ArrayList
,那么您可以使用无界通配符,如下所示:
public OpponentListAdapter(Context c, ArrayList<?> l) {}
这将适用于两个数组列表,或者使您的构造函数变得通用,并给出类型参数。
关于java - 以不同 ArrayList 类型作为参数的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21556925/