Code:
/**
* Implementation of a vector of elements based on an array buffer.
* It provides methods for dynamically adding and removing elements,
* and is capable of fast random access. Additionally, it provides
* an iterator to traverse through its elements.
*
* @author (Ihr Name)
* @version (eine Versionsnummer oder ein Datum)
*/
public class Vector <T>
{
/**
* The array buffer into which the elements of the vector are stored.
* The capacity of the vector is the length of this array buffer.
*/
private T[] elements;
/**
* The size of the vector (the number of elements it contains).
*/
private int size;
/**
* Constructs an empty vector with an initial capacity of 10.
*/
public Vector T()
{
this( 10 );
}
/**
* Constructs an empty vector with the specified initial capacity.
*
* @param capacity the initial capacity of the vector
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public Vector( int capacity )
{
if ( capacity >= 0 )
{
elements = new T[ capacity ];
size = 0;
}
}
/**
* Appends the specified element to the end of this vector.
* If the array has to be enlarged, then its capacity is increased by 10.
*
* @param element element to be appended to this vector
*/
public void add( int element )
{
++size;
if ( size > elements.length )
{
elements = java.util.Arrays.copyOf( elements, elements.length + 10 );
}
elements[ size - 1 ] = element;
}
/**
* Inserts the specified element at the specified position in this
* vector. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
* If the array has to be enlarged, then its capacity is increased by 10.
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IllegalIndexException if the index is out of range
* ({@code index < 0 || index > size()})
*/
public void add( int index, int element )
{
if ( index >= 0 && index <= size )
{
++size;
if ( size > elements.length )
{
elements = java.util.Arrays.copyOf( elements, elements.length + 10 );
}
for ( int i = size - 1; i > index; --i )
{
elements[ i ] = elements[ i - 1 ];
}
elements[ index ] = element;
}
}
/**
* Returns the capacity of this vector.
*
* @return the capacity of this vector
*/
public int capacity()
{
return elements.length;
}
/**
* Removes all of the elements from this vector. The vector will
* have a capacity of 10 elements after this method returns.
*/
public void clear()
{
elements = new int[ 10 ];
size = 0;
}
/**
* Returns <tt>true</tt> if this vector contains the specified element.
*
* @param element element whose presence in this vector is to be tested
* @return <tt>true</tt> if this vector contains the specified element
*/
public boolean contains( int element )
{
return indexOf( element ) >= 0;
}
/**
* Increases the capacity of this vector instance, if necessary,
* to ensure that it can hold at least the number of elements
* specified by the capacity argument. If the new capacity is smaller
* than the actual length of the underlying array, then the array is
* left unchanged.
*
* @param capacity the desired minimum capacity
*/
public void ensureCapacity( int capacity )
{
if ( capacity > elements.length )
{
elements = java.util.Arrays.copyOf( elements, capacity );
}
}
/**
* Returns the element at the specified position in this vector.
*
* @param index index of the element to return
* @return element at the specified index
* @throws IllegalIndexException if the index is out of range
* ({@code index < 0 || index >= size()})
*/
public int get( int index )
{
if ( index >= 0 && index < size )
{
return elements[ index ];
}
return 0;
}
/**
* Returns the index of the first occurrence of the specified element
* in this vector, or throws an ElementNotFound exception if this vector
* does not contain the element.
*
* @param element element whose index is to be obtained
* @return the index of the first occurrence of the specified element
* in this vector
* @throws ElementNotFoundException if the vector does not contain the element
*/
public int indexOf( int element )
{
for ( int i = 0; i < elements.length; ++i )
{
if ( element == elements[ i ] )
{
return i;
}
}
return -1;
}
/**
* Returns <tt>true</tt> if this vector contains no elements.
*
* @return <tt>true</tt> if this vector contains no elements
*/
public boolean isEmpty()
{
return size == 0;
}
/**
* Returns an iterator to traverse this vector.
*
* @return an iterator to traverse this vector
*/
public Iterator iterator()
{
return new Iterator();
}
/**
* Removes the element at the specified position in this vector.
* Shifts any subsequent elements to the left (subtracts one from their
* indices).
*
* @param index the index of the element to be removed
* @return the element that was removed from the vector
* @throws IllegalIndexException if the index is out of range
* ({@code index < 0 || index >= size()})
*/
public int remove( int index )
{
if ( index >= 0 && index < size )
{
int element = elements[ index ];
for ( int i = index; i < size - 1; ++i )
{
elements[ i ] = elements[ i + 1 ];
}
--size;
return element;
}
return 0;
}
/**
* Replaces the element at the specified position in this vector with
* the specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IllegalIndexException if the index is out of range
* ({@code index < 0 || index >= size()})
*/
public int set( int index, int element )
{
if ( index >= 0 && index < size )
{
int previousElement = elements[ index ];
elements[ index ] = element;
return previousElement;
}
return 0;
}
/**
* Returns the number of elements in this vector.
*
* @return the number of elements in this vector
*/
public int size()
{
return size;
}
/**
* Returns a string representation of this vector. The string
* representation consists of a list of the vector's elements
* in the order they are returned by its iterator, enclosed in
* square brackets (<tt>"Vector[]"</tt>). Adjacent elements
* are separated by comma. Elements are converted to strings
* as by {@link String#valueOf(Object)}.
*
* @return a string representation of this vector
*/
public String toString()
{
Iterator i = iterator();
StringBuilder sb = new StringBuilder();
sb.append( "Vector[" );
while ( i.hasNext() )
{
sb.append( i.next() );
if ( i.hasNext() )
{
sb.append( ',' );
}
}
return sb.append(']').toString();
}
/**
* Trims the capacity of this vector instance to be the vector's
* current size. An application can use this operation to minimize
* the storage of an vector instance.
*/
public void trimToSize()
{
if ( size < elements.length )
{
elements = java.util.Arrays.copyOf( elements, size );
}
}
/**
* An iterator to traverse this vector.
*
* @author (Ihr Name)
* @version (eine Versionsnummer oder ein Datum)
*/
public class Iterator
{
/**
* The index of the iterator in the vector.
*/
private int index;
/**
* Constructs an iterator positioned before the first element.
*/
public Iterator()
{
index = -1;
}
/**
* Checks, whether this iterator has a further element or not.
*
* @return true, if there is another element retrievable by <tt>next()</tt>
*/
public boolean hasNext()
{
return index < size - 1;
}
/**
* Returns the next element of this vector.
*
* @return the next element of this vector
*/
public int next()
{
++index;
return elements[ index ];
}
}
}
Hi und zwar sollen wir dies so umschreiben, dass die Klasse generische Datentypen verarbeiten kann!
Irgendwie kommt bei mir aber immer der Fehler generic array creation
Zuletzt bearbeitet: