分别用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 12:36:22 浏览: 15
好的,让我来逐步回答你的问题。
(1)首先定义一个类Student,代码如下:
```
public class Student {
private int sno;
private String name;
public Student(int sno, String name) {
this.sno = sno;
this.name = name;
}
public int getSno() {
return sno;
}
public String getName() {
return name;
}
}
```
(2)然后定义一个类Course,代码如下:
```
public class Course {
private int cno;
private String cname;
private int credit;
public Course(int cno, String cname, int credit) {
this.cno = cno;
this.cname = cname;
this.credit = credit;
}
public int getCno() {
return cno;
}
public String getCname() {
return cname;
}
public int getCredit() {
return credit;
}
public void setCredit(int credit) {
this.credit = credit;
}
}
```
(3)定义一个Vector对象stu和一个ArrayList对象cour,代码如下:
```
Vector<Student> stu = new Vector<>();
ArrayList<Course> cour = new ArrayList<>();
```
(4)向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, "张小明"));
```
(5)向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));
```
(6)用Iterator遍历stu中的元素,删除名字大于等于3个字的学生,代码如下:
```
Iterator<Student> iterator = stu.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if (student.getName().length() >= 3) {
iterator.remove();
}
}
```
(7)用数组下标遍历cour中的元素,将“数据结构”这门课程的学分从4学分改为2学分,代码如下:
```
for (int i = 0; i < cour.size(); i++) {
if (cour.get(i).getCname().equals("数据结构")) {
cour.get(i).setCredit(2);
}
}
```
以上就是使用Vector和ArrayList来存储和访问学生以及课程的相关信息的完整代码。