java面向对象的一维数组学生系统
时间: 2023-09-28 20:11:21 浏览: 56
好的,以下是一个简单的学生系统示例:
```java
public class StudentSystem {
private Student[] students; // 一维数组存储学生对象
private int numOfStudents; // 学生数量
public StudentSystem(int size) {
students = new Student[size];
numOfStudents = 0;
}
public void addStudent(Student student) {
if (numOfStudents < students.length) {
students[numOfStudents] = student;
numOfStudents++;
} else {
System.out.println("学生数量已达上限,无法添加!");
}
}
public void removeStudent(int index) {
if (index < 0 || index >= numOfStudents) {
System.out.println("无法删除该学生,索引越界!");
} else {
for (int i = index; i < numOfStudents - 1; i++) {
students[i] = students[i + 1];
}
numOfStudents--;
}
}
public void displayStudents() {
for (int i = 0; i < numOfStudents; i++) {
System.out.println("学生" + (i + 1) + "信息:");
System.out.println(students[i]);
}
}
}
```
上述代码中,我们定义了一个 `StudentSystem` 类,其中包含一个一维数组 `students` 存储学生对象,以及一个整型变量 `numOfStudents` 记录学生数量。我们通过构造方法 `public StudentSystem(int size)` 初始化数组大小,再通过 `public void addStudent(Student student)` 方法向数组中添加学生对象,通过 `public void removeStudent(int index)` 方法删除指定索引的学生对象,通过 `public void displayStudents()` 方法打印所有学生的信息。
接下来,我们还需要定义一个 `Student` 类来存储每个学生的信息:
```java
public class Student {
private String name; // 姓名
private int age; // 年龄
private String gender; // 性别
private String id; // 学号
public Student(String name, int age, String gender, String id) {
this.name = name;
this.age = age;
this.gender = gender;
this.id = id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getId() {
return id;
}
public String toString() {
return "姓名:" + name + "\n年龄:" + age + "\n性别:" + gender + "\n学号:" + id + "\n";
}
}
```
上述代码中,我们定义了一个 `Student` 类,其中包含四个私有属性 `name`、`age`、`gender`、`id` 分别存储学生的姓名、年龄、性别和学号。通过构造方法 `public Student(String name, int age, String gender, String id)` 初始化这些属性值,以及通过 getter 方法 `public String getName()、public int getAge()、public String getGender()、public String getId()` 获取属性值。最后,我们重写了 `toString()` 方法,用于打印学生对象的信息。
下面是一个简单的测试程序:
```java
public class TestStudentSystem {
public static void main(String[] args) {
StudentSystem system = new StudentSystem(3); // 创建学生系统对象,最多存储三个学生
system.addStudent(new Student("张三", 20, "男", "001"));
system.addStudent(new Student("李四", 21, "女", "002"));
system.addStudent(new Student("王五", 22, "男", "003"));
system.displayStudents(); // 打印所有学生信息
system.removeStudent(1); // 删除索引为1的学生
system.displayStudents(); // 再次打印所有学生信息
}
}
```
上述程序首先创建了一个 `StudentSystem` 对象,最多存储三个学生。接着,我们通过 `addStudent(new Student("张三", 20, "男", "001"))` 方法向学生系统中添加了三个学生对象。然后,我们通过 `displayStudents()` 方法打印了所有学生的信息。接着,我们通过 `removeStudent(1)` 方法删除了索引为1的学生对象,最后再次通过 `displayStudents()` 方法打印了所有学生的信息,可以看到索引为1的学生已被删除。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)