分别用Vector和ArrayList来存储和访问学生以及课程的相关信息。 (1)定义类Student,拥有两个属性:学号sno(int)和姓名name(String); (2)定义类Course,拥有三个属性:课程号cno(int),课程名称cname(String)和学分credit(int); (3)定义一个集合类Vector的对象stu,以及一个集合类ArrayList的对象cour。 (4)向stu中加入6个学生的实例[{1001, “张三”},{1002, “李四”}, {1003, “王五”}, {1004, “赵六”}, {1005, “赵小宝”}, {1006, “张小明”}] ; (5)向cour中加入4门课程的实例[{1, “Java程序设计”, 3}, {1, “, 3}, {2, “数据结构”, 4}, {3, “数据库原理”, 3}, {4, “计算机网络”, 4}]; (6)用Interator遍历stu中的元素,删除名字大于等于3个字的学生; (7)用数组下标遍历cour中的元素,将“数据结构”这门课程的学分从4学分改为2学分。
时间: 2024-03-23 16:36:28 浏览: 21
好的,以下是使用Vector和ArrayList来存储和访问学生以及课程的相关信息的Java代码实现:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
// 定义学生类
class Student {
int sno;
String name;
public Student(int sno, String name) {
this.sno = sno;
this.name = name;
}
}
// 定义课程类
class Course {
int cno;
String cname;
int credit;
public Course(int cno, String cname, int credit) {
this.cno = cno;
this.cname = cname;
this.credit = credit;
}
}
// 定义学生集合对象和课程集合对象
Vector<Student> stu = new Vector<>();
ArrayList<Course> cour = new ArrayList<>();
// 向stu中加入6个学生的实例
stu.add(new Student(1001, "张三"));
stu.add(new Student(1002, "李四"));
stu.add(new Student(1003, "王五"));
stu.add(new Student(1004, "赵六"));
stu.add(new Student(1005, "赵小宝"));
stu.add(new Student(1006, "张小明"));
// 向cour中加入4门课程的实例
cour.add(new Course(1, "Java程序设计", 3));
cour.add(new Course(2, "数据结构", 4));
cour.add(new Course(3, "数据库原理", 3));
cour.add(new Course(4, "计算机网络", 4));
// 用Iterator遍历stu中的元素,删除名字大于等于3个字的学生
Iterator<Student> it1 = stu.iterator();
while (it1.hasNext()) {
Student s = it1.next();
if (s.name.length() >= 3) {
it1.remove();
}
}
// 用数组下标遍历cour中的元素,将“数据结构”这门课程的学分从4学分改为2学分
for (int i = 0; i < cour.size(); i++) {
Course c = cour.get(i);
if (c.cname.equals("数据结构")) {
c.credit = 2;
cour.set(i, c);
}
}
}
}
```