⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flexvector.java

📁 java高级使用教程 全书一共分六章
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    /// Searches backwards for the specified object, starting from the last
    // position and returns an index to it. 
    // @param elem the desired element
    // @return the index of the element, or -1 if it was not found.
    public final int lastIndexOf( Object elem )
	{
	return lastIndexOf( elem, elementCount );
	}

    /// Searches backwards for the specified object, starting from the specified
    // position and returns an index to it. 
    // @param elem the desired element
    // @param index the index where to start searching
    // @return the index of the element, or -1 if it was not found.
    public final synchronized int lastIndexOf( Object elem, int index )
	{
	for ( int i = index ; --i >= 0 ; )
	    if ( elem.equals( elementData[elementOffset + i] ) )
		return i;
	return -1;
	}

    /// Returns the element at the specified index.
    // @param index the index of the desired element
    // @exception ArrayIndexOutOfBoundsException If an invalid 
    // index was given.
    public final synchronized Object elementAt( int index )
	{
	if ( index >= elementCount )
	    throw new ArrayIndexOutOfBoundsException(
		index + " >= " + elementCount );
	// Since try/catch is free, except when the exception is thrown,
	// put in this extra try/catch to catch negative indexes and
	// display a more informative error message.  This might not
	// be appropriate, especially if we have a decent debugging
	// environment - JPayne.
	try
	    {
	    return elementData[elementOffset + index];
	    }
	catch ( ArrayIndexOutOfBoundsException e )
	    {
	    throw new ArrayIndexOutOfBoundsException( index + " < 0" );
	    }
	}

    /// Returns the first element of the sequence.
    // @exception NoSuchElementException If the sequence is empty.
    public final synchronized Object firstElement()
	{
	if ( elementCount == 0 )
	    throw new NoSuchElementException();
	return elementData[elementOffset];
	}

    /// Returns the last element of the sequence.
    // @exception NoSuchElementException If the sequence is empty.
    public final synchronized Object lastElement()
	{
	if ( elementCount == 0 )
	    throw new NoSuchElementException();
	return elementData[elementOffset + elementCount - 1];
	}

    /// Sets the element at the specified index to be the specified object.
    // The previous element at that position is discarded.
    // @param obj what the element is to be set to
    // @param index the specified index
    // @exception ArrayIndexOutOfBoundsException If the index was invalid.
    public final synchronized void setElementAt( Object obj, int index )
	{
	if ( index >= elementCount )
	    throw new ArrayIndexOutOfBoundsException(
		index + " >= " + elementCount );
	elementData[elementOffset + index] = obj;
	}

    /// Deletes the element at the specified index. Elements with an index
    // greater than the current index are moved down.
    // @param index the element to remove
    // @exception ArrayIndexOutOfBoundsException If the index was invalid.
    public final synchronized void removeElementAt( int index )
	{
	if ( index == 0 )
	    {
	    // Special case for removing the first element.
	    elementData[elementOffset] = null;			// for gc
	    ++elementOffset;
	    --elementCount;
	    }
	else
	    {
	    if ( index >= elementCount )
		throw new ArrayIndexOutOfBoundsException(
		    index + " >= " + elementCount );
	    int j = elementCount - index - 1;
	    if ( j > 0 )
		System.arraycopy(
		    elementData, elementOffset + index + 1,
		    elementData, elementOffset + index, j );
	    --elementCount;
	    elementData[elementOffset + elementCount] = null;	// for gc
	    if ( elementCount == 0 )
		elementOffset = 0;
	    }
	}

    /// Inserts the specified object as an element at the specified index.
    // Elements with an index greater or equal to the current index 
    // are shifted up.
    // @param obj the element to insert
    // @param index where to insert the new element
    // @exception ArrayIndexOutOfBoundsException If the index was invalid.
    public final synchronized void insertElementAt( Object obj, int index )
	{
	if ( index >= elementCount + 1 )
	    throw new ArrayIndexOutOfBoundsException(
		index + " >= " + elementCount + 1);
	if ( index == 0 && elementOffset > 0 )
	    {
	    // Special case for inserting at the beginning.
	    --elementOffset;
	    elementData[elementOffset] = obj;
	    }
	else
	    {
	    ensureCapacity( elementCount + 1 );
	    System.arraycopy(
		elementData, elementOffset + index,
		elementData, elementOffset + index + 1, elementCount - index );
	    elementData[elementOffset + index] = obj;
	    }
	++elementCount;
	}

    /// Adds the specified object as the last element of the vector.
    // @param obj the element to be added
    public final synchronized void addElement( Object obj )
	{
	ensureCapacity( elementCount + 1 );
	elementData[elementOffset +  elementCount] = obj;
	++elementCount;
	}

    /// Removes the element from the vector. If the object occurs more
    // than once, only the first is removed. If the object is not an
    // element, returns false.
    // @param obj the element to be removed
    // @return true if the element was actually removed; false otherwise.
    public final synchronized boolean removeElement( Object obj )
	{
	int i = indexOf( obj );
	if ( i == -1 )
	    return false;
	removeElementAt( i );
	return true;
	}

    /// Removes all elements of the vector. The vector becomes empty.
    public final synchronized void removeAllElements()
	{
	for ( int i = 0; i < elementCount; ++i )
	    elementData[elementOffset + i] = null;
	elementCount = 0;
	elementOffset = 0;
	}

    /// Clones this vector. The elements are <strong>not</strong> cloned.
    public synchronized Object clone()
	{
	try
	    { 
	    FlexVector fv = (FlexVector) super.clone();
	    fv.elementData = new Object[elementCount];
	    fv.elementOffset = 0;
	    System.arraycopy(
		elementData, elementOffset, fv.elementData, 0, elementCount );
	    return fv;
	    }
	catch ( CloneNotSupportedException e )
	    {
	    // This shouldn't happen, since we are Cloneable.
	    throw new InternalError();
	    }
	}

    /// Converts the vector to a string. Useful for debugging.
    public final synchronized String toString()
	{
	int max = size() - 1;
	StringBuffer buf = new StringBuffer();
	Enumeration e = elements();
	buf.append( "[" );

	for ( int i = 0 ; i <= max ; ++i )
	    {
	    String s = e.nextElement().toString();
	    buf.append( s );
	    if ( i < max )
		buf.append( ", " );
	    }
	buf.append( "]" );
	return buf.toString();
	}
    }

final class FlexVectorEnumerator implements Enumeration
    {
    FlexVector flexvector;
    int count;

    FlexVectorEnumerator( FlexVector fv )
	{
	flexvector = fv;
	count = 0;
	}

    public boolean hasMoreElements()
	{
	return count < flexvector.size();
	}

    public Object nextElement()
	{
	synchronized ( flexvector )
	    {
	    if ( count < flexvector.size() )
		return flexvector.elementAt( count++ );
	    }
	throw new NoSuchElementException( "FlexVectorEnumerator" );
	}
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -