(1)创建一个ArrayList对象,添加本学期自己所学的所有课程。(2)输出列表的长度。 (3)输出列表第2个元素的信息。(4)删除第三个元素 (5)输出列表所有的元素信息。 (6)判断本学期是否可设课程名为java语言高级编程的课程。(7)向列表第2个位署添加一个课程对象。(8)使用迭代器遍历输出 题目中的ArrayList列表元素。 要求:定义课程Course类,属性包括课程号cid,课程名cname;学分 credit;方法:output(输出所有的属性值)
时间: 2024-02-29 21:51:54 浏览: 32
好的,以下是代码实现:
```java
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
// 创建 ArrayList 对象并添加课程
ArrayList<Course> courseList = new ArrayList<>();
courseList.add(new Course("001", "数学", 3));
courseList.add(new Course("002", "英语", 2));
courseList.add(new Course("003", "物理", 4));
courseList.add(new Course("004", "化学", 3));
courseList.add(new Course("005", "计算机科学", 4));
// 输出列表长度
System.out.println("列表长度:" + courseList.size());
// 输出第2个元素的信息
System.out.println("第2个元素信息:" + courseList.get(1));
// 删除第3个元素
courseList.remove(2);
// 输出所有元素的信息
System.out.println("所有元素信息:");
for (Course course : courseList) {
course.output();
}
// 判断是否存在课程名为java语言高级编程的课程
boolean isExist = false;
for (Course course : courseList) {
if (course.getCname().equals("java语言高级编程")) {
isExist = true;
break;
}
}
if (isExist) {
System.out.println("存在课程名为java语言高级编程的课程");
} else {
System.out.println("不存在课程名为java语言高级编程的课程");
}
// 向第2个位置添加一个课程
courseList.add(1, new Course("006", "Java语言高级编程", 4));
// 使用迭代器遍历输出所有元素的信息
System.out.println("所有元素信息(遍历):");
Iterator<Course> iterator = courseList.iterator();
while (iterator.hasNext()) {
Course course = iterator.next();
course.output();
}
}
}
class Course {
private String cid;
private String cname;
private int credit;
public Course(String cid, String cname, int credit) {
this.cid = cid;
this.cname = cname;
this.credit = credit;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getCredit() {
return credit;
}
public void setCredit(int credit) {
this.credit = credit;
}
public void output() {
System.out.println("课程号:" + cid + ",课程名:" + cname + ",学分:" + credit);
}
@Override
public String toString() {
return "Course{" +
"cid='" + cid + '\'' +
", cname='" + cname + '\'' +
", credit=" + credit +
'}';
}
}
```
这个程序会创建一个 ArrayList 对象,添加多个 Course 类型的对象,并进行各种操作,包括输出列表长度、输出第2个元素的信息、删除第3个元素、输出所有元素的信息、判断是否存在某个特定的课程等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)