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

📄 vector.java

📁 RPG冒险游戏
💻 JAVA
字号:
package CustomlUtil;

public final class Vector 
{
   protected Object elementData[];
   protected int elementCount;
   protected int capacityIncrement;
   public Vector(int initialCapacity, int capacityIncrement)
   {
		this.elementData = new Object[initialCapacity];
		this.capacityIncrement = capacityIncrement;
   }

   public Vector(int initialCapacity)
   {
	   this(initialCapacity, 0);
   }

   public Vector()
   {
	   this(10);
   }

   public int size()
   {
	   return elementCount;
   }

   public boolean isEmpty()
   {
	   return elementCount == 0;
   }

   public int indexOf(Object elem)
   {
	   return indexOf(elem, 0);
   }

   public 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;
   }

   public int lastIndexOf(Object elem)
   {
	   return lastIndexOf(elem, elementCount-1);
   }
   
   public int lastIndexOf(Object elem, int index)
   {
        if (index >= elementCount)
        {
	            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
        }
		if (elem == null)
		{
		    for (int i = index; i >= 0; i--)
		    {
				if (elementData[i]==null)
				{
				    return i;
				}
		    }
		} 
		else 
		{
		    for (int i = index; i >= 0; i--)
		    {
				if (elem.equals(elementData[i]))
				{
				    return i;
				}
		    }
		}
		return -1;
   }

    public Object elementAt(int index)
    {
		if (index >= elementCount)
		{
		    throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
		}
	
	        return elementData[index];
    }

   public Object firstElement()
   {
		if (elementCount == 0)
		{
		    throw new NullPointerException();
		}
		return elementData[0];
   }
  
   public Object lastElement()
   {
		if (elementCount == 0)
		{
		    throw new NullPointerException();
		}
		return elementData[elementCount - 1];
   }

   public void setElementAt(Object obj, int index)
   {
		if (index >= elementCount) 
		{
		    throw new ArrayIndexOutOfBoundsException(index + " >= " + 
							     elementCount);
		}
		elementData[index] = obj;
   }

    public void removeElementAt(int index)
    {
		if (index >= elementCount)
		{
		    throw new ArrayIndexOutOfBoundsException(index + " >= " + 
					     elementCount);
		}
		else if (index < 0)
		{
		    throw new ArrayIndexOutOfBoundsException(index);
		}
		int j = elementCount - index - 1;
		if (j > 0)
		{
		    System.arraycopy(elementData, index + 1, elementData, index, j);
		}
		elementCount--;
		elementData[elementCount] = null; /* to let gc do its work */
    }

   public void insertElementAt(Object obj, int index)
   {
	   	if (index > elementCount)
	   	{
		    throw new ArrayIndexOutOfBoundsException(index
							     + " > " + elementCount);
		}
	   	if(elementCount + 1 >= elementData.length)
	   	{
	   		Object newArray[] = new Object[capacityIncrement + elementCount];
	   		System.arraycopy(elementData, 0, newArray, 0, elementData.length);
	   		elementData = null;
	   		elementData = newArray;
	   	}
		System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
		elementData[index] = obj;
		elementCount++;
   }
   
   public void addElement(Object obj)
   {
	   if(elementCount + 1 >= elementData.length)
	   	{
	   		Object newArray[] = new Object[capacityIncrement + elementCount];
	   		System.arraycopy(elementData, 0, newArray, 0, elementData.length);
	   		elementData = null;
	   		elementData = newArray;
	   	}
	   
	   elementData[elementCount++] = obj;
   }

   public boolean removeElement(Object obj)
   {
		int i = indexOf(obj);
		if (i >= 0)
		{
		    removeElementAt(i);
		    return true;
		}
		return false;
    }

    public void removeAllElements()
    {
	     for (int i = 0; i < elementCount; i++)
	     {
		    elementData[i] = null;
	     }
		elementCount = 0;
    }

    public void clear()
    {
        removeAllElements();
    }
}

⌨️ 快捷键说明

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