问答题
【说明】
java.util库中提供了Vector模板类,可作为动态数组使用,并可容纳任意数据类型。该类的部分方法说明如下表所示:
方法名 | 含 义 |
add(k) | 向vector对象的尾部添加一个元素k |
removeElementAt(i) | 删除序号为i的元素(vector元素序号从0开始) |
isEmpty() | 判断vector对象是否含有元素 |
size() | 返回vector对象中所包含的元素个数 |
【Java代码】
import
(1) ;
public class JavaMain {
static private final int
(2) = 6;
public static void main(String[] args){
Vector<Integer> theVector = new Vector<
(3) >();
// 初始化 theVector, 将theVector的元素设置为0至5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.add(
(4) );
showVector(theVector); // 依次输出theVector中的元素
theVector.removeElementAt(3);
showVector(theVector);
}
public static void showVector(Vector<Integer> theVector
if (theVector.isEmpty()) {
System.out.println("theVectcr is empty.");
return;
}
for (int loop = 0; loop < theVector.size(); loop++)
System.out.print(theVector.get(loop));
System.out.print(", ");
}
System.out.println();
}
}
该程序运行后的输出结果为:
0,1,2,3,4,5
(5)