81,115
社区成员
发帖
与我相关
我的任务
分享
public class TestObj {
int count=0;
public TestObj() {
count+=5;
}
public int size(){
System.out.println("size="+count);
return count;
}
}
public class test{
public test(){}
public static void main(String[] args){
TestObj similarVector=new TestObj();
for(int i=1;i<similarVector.size();i++){
//do something
}
}
}
/**
* Returns the number of components in this vector.
*
* @return the number of components in this vector.
*/
protected int elementCount;
/**
* 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 the component to insert.
* @param index where to insert the new component.
* @exception ArrayIndexOutOfBoundsException if the index was invalid.
* @see #size()
* @see #add(int, Object)
* @see List
*/
public synchronized void insertElementAt(Object obj, int index) {
modCount++;
if (index >= elementCount + 1) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
elementCount++;
}
/**
* Returns the number of components in this vector.
*
* @return the number of components in this vector.
*/
public synchronized int size() {
return elementCount;
}