admin 管理员组

文章数量: 887031


2024年1月11日发(作者:魔兽争霸w3c欧服)

java学生表的增删改查

创建学生类(Student):

```java

public class Student {

private int id;

private String name;

private int age;

public Student(int id, String name, int age) {

= id;

= name;

= age;

}

// Getters and setters

public int getId() {

return id;

}

public void setId(int id) {

= id;

}

public String getName() {

return name;

}

public void setName(String name) {

= name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

= age;

}

// Override toString() method to print information

@Override

public String toString() {

return "Student [id=" + id + ", name=" + name + ", age=" +

age + "]";

}

}

```

创建学生管理类(StudentManager):

```java

import ist;

import ;

public class StudentManager {

private List students;

public StudentManager() {

students = new ArrayList();

}

// 添加学生

public void addStudent(Student student) {

(student);

}

// 删除学生

public void deleteStudent(int id) {

for (int i = 0; i < (); i++) {

if ((i).getId() == id) {

(i);

}

}

}

// 更新学生信息

public void updateStudent(Student student) {

for (int i = 0; i < (); i++) {

if ((i).getId() == ()) {

(i, student);

}

}

}

// 查找学生

public Student findStudent(int id) {

for (Student student : students) {

if (() == id) {

return student;

}

}

return null;

}

// 打印所有学生信息

public void printStudents() {

for (Student student : students) {

n(student);

}

}

}

```

使用示例:

```java

public class Main {

public static void main(String[] args) {

StudentManager studentManager = new StudentManager();

// 添加学生

Student student1 = new Student(1, "Alice", 20);

dent(student1);

Student student2 = new Student(2, "Bob", 22);

dent(student2);

// 打印所有学生信息

tudents();

// 更新学生信息

Student updatedStudent = new Student(2, "Bob Smith", 23);

Student(updatedStudent);

// 查找学生

Student foundStudent = udent(2);

if (foundStudent != null) {

n("Found student: " + foundStudent);

} else {

n("Student not found.");

}

// 删除学生

Student(1);

// 打印所有学生信息

tudents();

}

}

```

在上述示例中,我们创建了一个Student类来表示学生对象,并创建了一个StudentManager类来管理学生表。通过StudentManager类的方法,我们可以添

加、删除、更新和查找学生。最后,我们在Main类中演示了如何使用StudentManager类来进行学生表的增删改查操作。


本文标签: 学生 类来 创建 信息