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

📄 flexvector.java

📁 java高级使用教程 全书一共分六章
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// FlexVector - improved version of java.util.Vector
//
// Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>.  All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// Visit the ACME Labs Java page for up-to-date versions of this and other
// fine Java utilities: http://www.acme.com/java/
//
//
// Original copyright notice:
//
// @(#)Vector.java	1.29 95/12/01  
//
// Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
//
// Permission to use, copy, modify, and distribute this software
// and its documentation for NON-COMMERCIAL purposes and without
// fee is hereby granted provided that this copyright notice
// appears in all copies. Please refer to the file "copyright.html"
// for further important copyright and licensing information.
//
// SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
// ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
// DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.

package Acme;

import java.util.*;

/// Improved version of java.util.Vector.
// <P>
// Usage is identical to java.util.Vector.  The changes are all internal.
// So far the only change is that removing elements from the beginning
// of the vector is handled much more efficiently.
// <P>
// Note that this can't be a subclass of Vector because Vector is full
// of final methods, which can't be overridden.
// <P>
// <A HREF="/resources/classes/Acme/FlexVector.java">Fetch the software.</A><BR>
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>

public class FlexVector implements Cloneable
    {

    /// The buffer where elements are stored.
    /*private*/ protected Object elementData[];

    /// The number of elements in the buffer.
    /*private*/ protected int elementCount;

    /// The offset for the first element.
    /*private*/ protected int elementOffset;

    /// The size of the increment. If it is 0 the size of the
    // the buffer is doubled everytime it needs to grow.
    /*private*/ protected int capacityIncrement;

    /// Constructs an empty vector with the specified storage
    // capacity and the specified capacityIncrement.
    // @param initialCapacity the initial storage capacity of the vector
    // @param capacityIncrement how much to increase the element's 
    // size by.
    public FlexVector( int initialCapacity, int capacityIncrement )
	{
	elementData = new Object[initialCapacity];
	elementCount = 0;
	elementOffset = 0;
	this.capacityIncrement = capacityIncrement;
	}

    /// Constructs an empty vector with the specified storage capacity.
    // @param initialCapacity the initial storage capacity of the vector
    public FlexVector( int initialCapacity )
	{
	this( initialCapacity, 0 );
	}

    /// Constructs an empty vector.
    public FlexVector()
	{
	this( 10 );
	}


    /// Copies the elements of this vector into the specified array.
    // @param anArray the array where elements get copied into
    public final synchronized void copyInto( Object anArray[] )
	{
	for ( int i = 0; i < elementCount; ++i )
	    anArray[i] = elementData[elementOffset + i];
	}

    /// Trims the vector's capacity down to size. Use this operation to
    // minimize the storage of a vector. Subsequent insertions will
    // cause reallocation.
    public final synchronized void trimToSize()
	{
	int oldCapacity = elementData.length;
	if ( elementCount < oldCapacity )
	    {
	    Object oldData[] = elementData;
	    elementData = new Object[elementCount];
	    System.arraycopy(
		oldData, elementOffset, elementData, 0, elementCount );
	    elementOffset = 0;
	    }
	}

    /// Ensures that the vector has at least the specified capacity.
    // @param minCapacity the desired minimum capacity
    public final synchronized void ensureCapacity( int minCapacity )
	{
	int oldTotalCapacity = elementData.length;
	int oldUsableCapacity = oldTotalCapacity - elementOffset;
	if ( oldUsableCapacity < minCapacity )
	    {
	    if ( oldTotalCapacity >= minCapacity )
		{
		// Don't have to make a new object, we can just compactify.
		System.arraycopy(
		    elementData, elementOffset, elementData, 0, elementCount );
		for ( int i = elementCount;
		      i < elementCount + elementOffset; ++i )
		    elementData[i] = null;
		}
	    else
		{
		Object oldData[] = elementData;
		int newCapacity = ( capacityIncrement > 0 ) ?
		    ( oldTotalCapacity + capacityIncrement ) :
		    ( oldTotalCapacity * 2 );
		if ( newCapacity < minCapacity )
		    newCapacity = minCapacity;
		elementData = new Object[newCapacity];
		System.arraycopy(
		    oldData, elementOffset, elementData, 0, elementCount );
		}
	    elementOffset = 0;
	    }
	}

    /// Sets the size of the vector. If the size shrinks, the extra elements
    // (at the end of the vector) are lost; if the size increases, the
    // new elements are set to null.
    // @param newSize the new size of the vector
    public final synchronized void setSize( int newSize )
	{
	if ( newSize > elementCount )
	    ensureCapacity( newSize );
	else
	    for ( int i = newSize ; i < elementCount ; ++i )
		elementData[elementOffset + i] = null;
	elementCount = newSize;
	if ( elementCount == 0 )
	    elementOffset = 0;
	}

    /// Returns the current capacity of the vector.
    public final int capacity()
	{
	return elementData.length;
	}

    /// Returns the number of elements in the vector.
    // Note that this is not the same as the vector's capacity.
    public final int size()
	{
	return elementCount;
	}

    /// Returns true if the collection contains no values.
    public final boolean isEmpty()
	{
	return elementCount == 0;
	}

    /// Returns an enumeration of the elements. Use the Enumeration methods on
    // the returned object to fetch the elements sequentially.
    public final synchronized Enumeration elements()
	{
	return new FlexVectorEnumerator( this );
	}
    
    /// Returns true if the specified object is a value of the collection.
    // @param elem the desired element
    public final boolean contains( Object elem )
	{
	return indexOf( elem, 0 ) >= 0;
	}

    /// Searches for the specified object, starting from the first 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 indexOf( Object elem )
	{
	return indexOf( elem, 0 );
	}

    /// Searches for the specified object, starting at 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 indexOf( Object elem, int index )
	{
	for ( int i = index ; i < elementCount ; ++i )
	    if ( elem.equals( elementData[elementOffset + i] ) )
		return i;
	return -1;
	}

⌨️ 快捷键说明

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