कोड पर विचार करें:अनुमानित प्रकार एक तुलनीय सामान्य प्रकार के लिए एक वैध विकल्प नहीं है
Bound mismatch: The generic method sort(List< T >) of type Collections is not applicable for the arguments (ArrayList< MyItem< T > >). The inferred type MyItem< T > is not a valid substitute for the bounded parameter < T extends Comparable< ? super T > >
क्यों यह गलत है:
public abstract class Item<T> implements Comparable<T>
{
protected T item;
public int compareTo(T o)
{
return 0; // this doesn't matter for the time being
}
}
public class MyItem<T> extends Item<String>
{
T object;
}
public class Foo<T>
{
protected ArrayList<T> list;
}
public class Bar<V> extends Foo<MyItem<V>>
{
public void sort()
{
Collections.sort(list);
}
}
तरह कॉल त्रुटि देता है?
यदि MyItem<V>
Comparable
लागू करता है तो यह एक विकल्प क्यों नहीं है?
क्षमा करें अगर यह पूछा गया है, लेकिन मुझे लगता है कि सवाल कुछ हद तक विशिष्ट है।
java: no suitable method found for
sort(java.util.ArrayList<MyItem<V>>
)method
java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>)
is not applicable (cannot instantiate from arguments because actual and formal argument lists differ in length)method
java.util.Collections.<T>sort(java.util.List<T>)
is not applicable (inferred type does not conform to declared bound(s) inferred:MyItem<V>
bound(s):java.lang.Comparable<? super MyItem<V>>
)
तो, मुख्य सवाल यह है::
यही पर हाजिर था:
करने के लिए अपने
Item<T>
वर्ग बदलें। चीयर्स! स्पष्टीकरण के लिए, मैंने अतिरिक्त कक्षाएं शामिल कीं क्योंकि मेरे द्वारा प्रदान किया गया कोड मेरे वास्तविक कोड का एक अधिक सरलीकृत संस्करण है, और मुझे यकीन नहीं था कि समस्या कहां थी। मैं पूरी तरह से चूक गया कि मैं सिर्फ वास्तविक तुलना ही बदल सकता हूं। एक बार फिर धन्यवाद। – terrorcellबेहतर: 'सार्वजनिक अमूर्त वर्ग आइटम लागू तुलनात्मक आइटम > '- नहीं बढ़ाता है? –