java中将类添加到Vector后面,类中有多元素的一个Vector,只有最后一个元素输出
这是 school 类中的一个函数,用来向student类中添加一条记录
info数组的格式为 姓名 课程名 分数 课程名 分数 课程名 分数 ....
school类中,有个stu的Vector, 还有个stemp,都是class student类型
下面的代码是添加一条记录,
// Vector <student> stu = new Vector <student>
// student stemp = new student();
public void addstu(String[] info){
stemp.name = info[0]; //学生姓名
//感觉这一步出了问题
stemp.addcou(info); //添加课程 函数代码在下面
stu.addElement(stemp); //把做好的类放到Vector最后
System.out.println(stemp.cou.elementAt(0).c_name);
int i = 0;
while(i<3){
System.out.println("-----1-----");
System.out.println(stemp.name);
System.out.println(stu.elementAt(0).cou.elementAt(i).c_name);
System.out.println(stemp.cou.elementAt(i).c_name);
i++;
}
}
// Vector <course> cou = new Vector <course>
// course ctemp = new course();
public void addcou(String[] info){
int length = info.length;
int i = 1;
while(i<length){
ctemp.c_name = info[i]; //存入课程名
ctemp.mark = Integer.parseInt(info[i+1]); //存入成绩
i+=2;
cou.addElement(ctemp); //在结尾添加ctemp
System.out.println("-----2-----");
System.out.println(cou.elementAt((i-2)/2).c_name);
cou_num++;
}
}