public class MyVectorIndexOutOfBoundException extends ArrayIndexOutOfBoundsException
{
public MyVectorIndexOutOfBoundException( String msg )
{ super(msg);
}
public MyVectorIndexOutOfBoundException( int arg )
{
super(arg);
}
public MyVectorIndexOutOfBoundException()
{
super();
}
}在JCRT中调试的时候 出错。
出错信息为:MyVectorIndexOutOfBoundException.java是公共的,应在名MyVectorIndexOutOfBoundException.java的文件中声明
{
public MyVectorIndexOutOfBoundException( String msg )
{ super(msg);
}
public MyVectorIndexOutOfBoundException( int arg )
{
super(arg);
}
public MyVectorIndexOutOfBoundException()
{
super();
}
}在JCRT中调试的时候 出错。
出错信息为:MyVectorIndexOutOfBoundException.java是公共的,应在名MyVectorIndexOutOfBoundException.java的文件中声明
我的文件已经是JAVA后缀的啦,你说的前后的两个文件名有什么区别吗?能详细一点么?我实在不明白?
* 创建日期: 2006-5-25
* 版 本: 1.0
*/
package work;import java.io.Serializable;
import java.util.AbstractList;
import java.util.Collection;
import java.util.List;/**
*
*/
public class MyVector
extends AbstractList
implements List, Cloneable, Serializable {
protected Object elementData[]; protected int elementCount; protected int capacityIncrement;
/**
* Constructs a vector containing the elements of the specified collection,
* in the order they are returned by the collection's iterator.
* @param c the collection whose elements are to be placed into this vector.
*/
public MyVector( Collection c ){
elementCount = c.size();
// 10% for growth
elementData = new Object[
(int)Math.min((elementCount*110L)/100,Integer.MAX_VALUE)];
c.toArray(elementData);
}
/**
* constructs an empty vector with specified initial capacity and capacity increment.
* @param initialCapacity the initial capacity of the vector.
* @param capacityIncrement the amount by which the capacity is increased when the vector overflows.
*/
public MyVector( int initialCapacity, int capacityIncrement ){
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
/**
* constructs an empty vector with specified initial capacity and with its capacity increment equal to one.
* @param initialCapacity the initial capacity of the vector.
*/
public MyVector( int initialCapacity ){
this(initialCapacity, 1);
}
/**
* insert the specified object at the specified position in this vector.
* (Note, position start from 0);
* @param int index index at which the specified element is to be inserted.
* @param element element to be inserted.
*/
public void add(int index, Object element) {
insertElementAt(element, index);
}
/**
* Tests if the specified object is a component in this vector.
* @param Object elem the specified object
*/
public boolean contains(Object elem) {
return indexOf(elem, 0) >= 0;
}
/**
* compares the specified Object with this Vector for equality;
*/
public synchronized boolean equals(Object o) {
return super.equals(o);
}
/**
* returns the element at the specified position in this vector.
* @int index index of element to return.
* Throws MyVectorIndexOutOfBoundException for incorrect index;
*/
public synchronized Object get(int index) {
if (index >= elementCount)
throw new MyVectorIndexOutOfBoundException(index); return elementData[index];
}
/**
* Sets the component at the specified index of this vector to be the specified object.
* @param obj what the component is to be set to.
* @param index the specified index.
*/
public synchronized void setElementAt(Object obj, int index) {
if (index >= elementCount) {
throw new MyVectorIndexOutOfBoundException(index + " >= " +
elementCount);
}
elementData[index] = obj;
}
/**
* return the element at the specified position and remove it from the vector.
* Throws MyVectorIndexOutOfBoundException for incorrect index;
* @param index the index of the element to removed.
*/
public synchronized Object remove(int index) {
//The number of times this list has been structurally modified.
modCount++;
if (index >= elementCount)
throw new MyVectorIndexOutOfBoundException(index);
Object oldValue = elementData[index]; int numMoved = elementCount - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--elementCount] = null; // Let gc do its work return oldValue;
}
/**
* Trims the capacity of this vector to be the vector's current size. (group work)
*
*/
public synchronized void trimToSize() {
//The number of times this list has been structurally modified.
modCount++;
int oldCapacity = elementData.length;
if (elementCount < oldCapacity) {
Object oldData[] = elementData;
elementData = new Object[elementCount];
System.arraycopy(oldData, 0, elementData, 0, elementCount);
}
}
/**
* Appends the specified element to the end of this Vector.
* @param Object o the specified element
*/
public synchronized boolean add(Object o) {
//The number of times this list has been structurally modified.
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = o;
return true;
}
/**
* returns the number of elements in this vector; (group work)
*/
public synchronized int size() {
return elementCount;
}
/**
* Returns an array containing all of the elements in this Vector in the correct order.
*
*/
public synchronized Object[] toArray() {
Object[] result = new Object[elementCount];
System.arraycopy(elementData, 0, result, 0, elementCount);
return result;
}
/**
* Removes the first (lowest-indexed) occurrence of the argument from this vector. (group work)
* @param obj
* @return
*/
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj);
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
/**
* Deletes the component at the specified index. Each component in
* this vector with an index greater or equal to the specified
* <code>index</code> is shifted downward to have an index one
* smaller than the value it had previously. The size of this vector
* is decreased by <tt>1</tt>.<p>
*
* The index must be a value greater than or equal to <code>0</code>
* and less than the current size of the vector. <p>
*
* This method is identical in functionality to the remove method
* (which is part of the List interface). Note that the remove method
* returns the old value that was stored at the specified position.
*
* @param index the index of the object to remove.
* @exception ArrayIndexOutOfBoundsException if the index was invalid.
* @see #size()
* @see #remove(int)
* @see List
*/
private synchronized void removeElementAt(int index) {
//The number of times this list has been structurally modified.
modCount++;
if (index >= elementCount) {
throw new MyVectorIndexOutOfBoundException(index + " >= " + elementCount);
}
else if (index < 0) {
throw new MyVectorIndexOutOfBoundException(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 */
}
/**
* Searches for the first occurence of the given argument, beginning
* the search at <code>index</code>, and testing for equality using
* the <code>equals</code> method.
*
* @param elem an object.
* @param index the non-negative index to start searching from.
* @return the index of the first occurrence of the object argument in
* this vector at position <code>index</code> or later in the
* vector, that is, the smallest value <tt>k</tt> such that
* <tt>elem.equals(elementData[k]) && (k >= index)</tt> is
* <tt>true</tt>; returns <code>-1</code> if the object is not
* found. (Returns <code>-1</code> if <tt>index</tt> >= the
* current size of this <tt>Vector</tt>.)
* @exception IndexOutOfBoundsException if <tt>index</tt> is negative.
* @see Object#equals(Object)
*/
private synchronized 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;
}
/**
* Inserts the specified object as a component in this vector at the
* specified <code>index</code>. Each component in this vector with
* an index greater or equal to the specified <code>index</code> is
* shifted upward to have an index one greater than the value it had
* previously. <p>
*
* The index must be a value greater than or equal to <code>0</code>
* and less than or equal to the current size of the vector. (If the
* index is equal to the current size of the vector, the new element
* is appended to the Vector.)<p>
*
* This method is identical in functionality to the add(Object, int) method
* (which is part of the List interface). Note that the add method reverses
* the order of the parameters, to more closely match array usage.
* @param obj object to be inserted.
* @param index where to insert the new component.
*/
private synchronized void insertElementAt(Object obj, int index) {
//The number of times this list has been structurally modified.
modCount++;
if (index >= elementCount + 1) {
throw new MyVectorIndexOutOfBoundException(index + " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
elementCount++;
}
/**
* This implements the unsynchronized semantics of ensureCapacity.
* Synchronized methods in this class can internally call this
* method for ensuring capacity without incurring the cost of an
* extra synchronization.
*
* @see java.util.Vector#ensureCapacity(int)
*/
private void ensureCapacityHelper(int minCapacity) {
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement) : (oldCapacity * 2);
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
elementData = new Object[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, elementCount);
}
}
}