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

📄 quicklist.java

📁 用J2ME实现的
💻 JAVA
字号:
/*
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Copyright(c) 2003-2004 Jordi Martin Perez
 * 
 * This class is based on the QuickList example given in the article
 * http://www.microjava.com/articles/techtalk/object_lists written by Lee Miles
 * (lee_miles@my-deja.com)
 */
package org.piratis.j2me.core;

import java.util.Enumeration;

/**
 * Provides a QuickList (Vector/ArrayList) implementation
 * 
 * @author		Jordi Mart韓
 * @copyright	Copyright (c) 2003-2004
 * @created 	01-jun-2004
 * @version		$Id: QuickList.java,v 1.6 2004/07/10 15:44:06 piratis Exp $
 * 
 */

public class QuickList
{
    /**
     * Default initial size
     */
    private static final int INITIAL_SIZE = 4;

    /**
     * Object list
     */
    private Object[] list;

    /**
     * Pointer to the next element (mightn't be length-1!)
     */
    private int nextElement;

    /**
     * Enumeration implementor 
     */
    public class Enum
    	implements Enumeration
    {
        /**
         * Inner counter / pointer
         */
        private int innerCounter;
        
        /**
         * Private because only QuickList may create it!
         */
        private Enum()
        {
            this(0);
        }
        
        /**
         * Constructor which indicates where to begin
         * @param newPosition where to begin
         */
        private Enum(int newPosition)
        {
            this.innerCounter = newPosition;
        }
        
        /**
         * @see java.util.Enumeration#hasMoreElements()
         */
        public boolean hasMoreElements()
        {
            return this.innerCounter < QuickList.this.nextElement;
        }

        /**
         * @see java.util.Enumeration#nextElement()
         */
        public Object nextElement()
        {
            return QuickList.this.list[this.innerCounter++];
        }
    }
    /**
     * Creates a quick list with an initial array size
     */
    public QuickList()
    {
        this(QuickList.INITIAL_SIZE);
    }

    /**
     * Creates a QuickList with an initial allocated space
     * 
     * @param initialSize
     */
    public QuickList(int initialSize)
    {
        this.list = new Object[initialSize];
        this.nextElement = 0;
    }

    /**
     * Adds a new object to the QuickList
     * 
     * @param newObject the newObject
     */
    public final void add(Object newObject)
    {
        if (this.nextElement == this.list.length)
            ensureCapacity(this.list.length * 2);

        this.list[this.nextElement++] = newObject;
    }
    
    /**
     * Inserts the specified object as a component in this vector at 
     * the specified index. Each component in this vector with an index 
     * greater or equal to the specified index is shifted upward to 
     * have an index one greater than the value it had previously.<br>
     * <br>
     * The index must be a value greater than or equal to 0 and less 
     * than or equal to the current size of the vector.
     * @param newObject the component to insert
     * @param newPosition where to insert
     * @throws IndexOutOfBoundsException if the index was invalid
     */
    public final void insertElementAt(Object newObject, int newPosition)
    	throws IndexOutOfBoundsException
    {
        // check exceptions
        if (newPosition < 0 || newPosition >= this.nextElement) 
		    throw new IndexOutOfBoundsException("list[" + newPosition + 
		            "]	is out of bounds (max " + this.nextElement + ")");
        // get size if needed
        if (this.nextElement == this.list.length)
            ensureCapacity(this.list.length * 2);
        // move right part
        System.arraycopy(this.list, newPosition, this.list, newPosition + 1, this.nextElement - newPosition);
        // new element
        this.list[newPosition] = newObject;
    }
    
    /**
     * Sets the object at the given index. The previous one at that
     * position is removed from the QuickList.
     * @param newObject new component to set
     * @param position where to set this component
     * @throws IndexOutOfBoundsException
     */
    public final void setElementAt(Object newObject, int position)
    	throws IndexOutOfBoundsException
    {
        // check exceptions
        if (position < 0 || position >= this.nextElement) 
		    throw new IndexOutOfBoundsException("list[" + position + 
		            "]	is out of bounds (max " + this.nextElement + ")");
        
        this.list[position] = newObject;
    }

    /**
     * Returns the object at the given position
     * @param intPosition the position
     * @return the object
     * @throws IndexOutOfBoundsException
     * @throws NullPointerException
     */
	public final Object elementAt(int intPosition) 
		throws IndexOutOfBoundsException, NullPointerException 
	{
	    // check exceptions
	    if (intPosition < 0 || intPosition >= this.nextElement) 
	        throw new IndexOutOfBoundsException("list[" + intPosition + 
	                "]	is out of bounds (max " + this.nextElement + ")");
	    if (this.list[intPosition] == null)
	        throw new NullPointerException("list[" + intPosition + "] is null");
	    // return element
	    return this.list[intPosition];
	}
	
	/**
	 * Gets the number of objects within the list
	 * @return the number of objects
	 */
    public final int length() 
    {
       	return this.nextElement;
    }
    
    /**
     * Gets all the elements of the list
     * @return the whole collection of objects
     */
    public final Enumeration elements()
    {
        return new QuickList.Enum();
    }

    /**
     * Gets all the elements of the list from one position
     * @param position initial offset
     * @return the whole collection of objects
     */
    public final Enumeration elements(int position)
    {
        return new QuickList.Enum(position);
    }
    
    /**
     * Removes the first occurrence of the argument from this quicklist.
     * If the object is found in this quicklist, each component in the 
     * quicklist with an index greater or equal to the object's index 
     * is shifted downward to have an index one smaller than the 
     * value it had previously.     
     * @param obj the object to be removed
     * @return true if the argument was a component of this quicklist;
     * 		false otherwise.
     */
    public final boolean removeElement(Object obj)
    {
        int pos = indexOf(obj);
        if (pos >= 0)
        {
            removeElementAt(pos);
            return true;
        }
        else
            return false;
    }
    
    /**
     * Deletes the component at the specified index. Each component 
     * in this quicklist with an index greater or equal to the specified 
     * index is shifted downward to have an index one smaller than the 
     * value it had previously.<br>
     * <br>
     * The index must be a value greater than or equal to 0 and less 
     * than the current size of the quicklist. 
     * 
     * @param position where the object to be remove is located
     * @throws IndexOutOfBoundsException if the index was invalid
     */
    public final void removeElementAt(int position)
    	throws IndexOutOfBoundsException
    {
	    // check exceptions
	    if (position < 0 || position >= this.nextElement) 
	        throw new IndexOutOfBoundsException("list[" + position + 
	                "]	is out of bounds (max " + this.nextElement + ")");
	    System.arraycopy(this.list, position+1, this.list, position, this.nextElement - position - 1);
	    this.nextElement--;
    }

    /**
     * Remove all the objects of the list
     */
    public final void removeAllElements()
    {
        // reinitialize
        this.nextElement = 0;
        if (this.list.length > QuickList.INITIAL_SIZE)
            this.list = new Object[INITIAL_SIZE];
        for (int i=0; i<INITIAL_SIZE; i++)
            this.list[i] = null;
        // get disposed memory!
        System.gc();
    }

    /**
     * Tests if the specified object is a component in this vector.
     * @param obj an object
     * @return <code>true</code> if the specified object is a component 
     * 	in this vector; <code>false</code> otherwise.
     */
    public final boolean contains(Object obj)
    {
        return indexOf(obj) >= 0;
    }
    
    /**
     * Searches for the first occurence of the given argument, 
     * testing for equality using the equals method.
     * @param obj an object 
     * @return the index of the first occurrence of the object 
     * 		argument in this quicklist at position index or later 
     * 		in the quicklist; returns -1 if the object is not found.
     */
    public final int indexOf(Object obj)
    {
        int pos = 0;
        for (; pos<this.nextElement; pos++)
            if (this.list[pos].equals(obj)) break;
        if (pos >= this.nextElement) pos = -1;
        return pos;
    }

    /**
     * Increases the capacity of this quicklist, if necessary, to ensure 
     * that it can hold at least the number of components specified 
     * by the minimum capacity argument.
     * @param minCapacity the desired minimum capacity.
     */
    public final void ensureCapacity(int minCapacity)
    {
        if (this.length() < minCapacity)
        {
            Object[] newArray = new Object[minCapacity];
            System.arraycopy(this.list, 0, newArray, 0, this.nextElement);
            this.list = newArray;
        }
    }
}

⌨️ 快捷键说明

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