问题 问答题

【说明】
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)

答案

参考答案:

(1) java.util.Vector,或java.util.* (2) ARRAY_SIZE (3) Integer (4) cEachItem (5) 0,1,2,4,5

解析:

本题主要考查Java语言的基本使用和类库的应用。 在使用Java库中所提供的类时,一般需要导入该类库所处的包。所以,空(1)需要填入Vector类所在的包。空(2)处主要考查变量在使用前需要先定义的基本概念,后续的代码中使用了ARRAY_SIZE变量,但其使用前没有定义,因此,空(2)处应该为该变量的定义。Java中Vector模板类可存储任意类型,在定义Vector模板类的对象时,需要指定Vector对象的类型。从后面的代码可以看出,Vector被用于存储整型数,所以,空(3)处应填写整型。初始化代码将0到5共6个整数存储到theVector对象中,所以,空(4)处将循环变量的值存入theVector中。程序运行时将首先输出0至5,其次会删除第3个元素,再次输出时将不再包含整数3。

单项选择题
填空题