Ich erhalte die folgende Warnung von meiner NetBeans IDE.Verdächtiger Aufruf von java.util.Collection.contains
Suspicious call to java.util.Collection.contains
Expected type T, actual type Object
Darf ich wissen, was das bedeutet?
Das ergibt für mich keinen Sinn. Die Methoden List
und Collection
der Klasse contains
verwenden Object als ihren Methodenparameter.
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
*
* @author yan-cheng.cheok
*/
public abstract class AbstractCollection<T> implements Collection<T> {
protected List<T> list = new ArrayList<T>();
public boolean contains(Object o) {
// Suspicious call to java.util.Collection.contains
// Expected type T, actual type Object
return list.contains(o);
}
-Code-Schnipsel aus Klasse Collection
/**
* Returns <tt>true</tt> if this collection contains the specified element.
* More formally, returns <tt>true</tt> if and only if this collection
* contains at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this collection is to be tested
* @return <tt>true</tt> if this collection contains the specified
* element
* @throws ClassCastException if the type of the specified element
* is incompatible with this collection (optional)
* @throws NullPointerException if the specified element is null and this
* collection does not permit null elements (optional)
*/
boolean contains(Object o);
Code-Snippet aus List-Klasse
/**
* Returns <tt>true</tt> if this list contains the specified element.
* More formally, returns <tt>true</tt> if and only if this list contains
* at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this list is to be tested
* @return <tt>true</tt> if this list contains the specified element
* @throws ClassCastException if the type of the specified element
* is incompatible with this list (optional)
* @throws NullPointerException if the specified element is null and this
* list does not permit null elements (optional)
*/
boolean contains(Object o);
Wäre es nicht besser, die 'Object' Deklaration zu' T' zu ändern? – Bobby
Ja, du hast recht, fest. – dpsthree
@Bobby: Eigentlich ist es eine Implementierung der abstrakten Methode definiert in 'Collection' Schnittstelle. Daher müssen wir bei der Erklärung bleiben. –