java清空学生信息代码
时间: 2024-06-24 22:00:44 浏览: 8
在Java中,如果你有一个包含学生信息的类,比如`Student`,并且它有相关的属性(如姓名、年龄、学号等),你可以创建一个方法来清空这些属性。假设有一个`ArrayList<Student>`来存储学生,你可以这样做:
```java
import java.util.ArrayList;
class Student {
private String name;
private int age;
// 其他属性...
// getters和setters省略
public void clearStudentInfo() {
this.name = "";
this.age = 0; // 如果年龄允许为0
// 清空其他属性...
}
}
public class Main {
private ArrayList<Student> students;
public void setUp() {
// 初始化students列表
students = new ArrayList<>();
// 添加学生...
}
public void emptyStudentInfo() {
for (Student student : students) {
student.clearStudentInfo();
}
}
// 主程序入口或其他方法...
}
```
在这个例子中,`emptyStudentInfo()`方法遍历`students`列表,并调用每个`Student`对象的`clearStudentInfo()`方法来清空其信息。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)