java.util.Vector hat gesagt.:Java:... public boolean contains(Object elem) { return indexOf(elem, 0) >= 0; } ... public synchronized int indexOf(Object elem, int index) { if (elem == null) { for (int i = index ; i < elementCount ; i++) if (elementData[i]==null) return i; } else { for (int i = index ; i < elementCount ; i++) if (elem.equals(elementData[i])) return i; } return -1; }...
/**
* Searches for the first occurence of the given argument, beginning
* the search at <code>index</code>, and testing for equality using
* the <code>equals</code> method.
*
* @param elem an object.
* @param index the non-negative index to start searching from.
* @return the index of the first occurrence of the object argument in
* this vector at position <code>index</code> or later in the
* vector, that is, the smallest value <tt>k</tt> such that
* <tt>elem.equals(elementData[k]) && (k >= index)</tt> is
* <tt>true</tt>; returns <code>-1</code> if the object is not
* found. (Returns <code>-1</code> if <tt>index</tt> >= the
* current size of this <tt>Vector</tt>.)
* @exception IndexOutOfBoundsException if <tt>index</tt> is negative.
* @see Object#equals(Object)
*/
public synchronized int indexOf(Object elem, int index) {
if (elem == null) {
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = index ; i < elementCount ; i++)
if (elem.equals(elementData[i]))
return i;
}
return -1;
}
Hey Leute,
ich müsste wissen wie die Komplexität bei der Suche in Vector ausschaut.
Welcher Algorithmus steckt hinter der Suche? Ist es etwa eine lineare Suche?