写一个java代码的增删改查
时间: 2023-03-28 22:01:19 浏览: 65
好的,以下是一个简单的 Java 代码示例,展示了如何进行增删改查的操作:
```
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// 创建一个学生列表
ArrayList<Student> students = new ArrayList<>();
// 添加学生
Student student1 = new Student(1, "张三", 18);
students.add(student1);
// 修改学生信息
student1.setName("李四");
// 删除学生
students.remove(student1);
// 查询学生
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName() + " " + student.getAge());
}
}
}
class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
上述代码创建了一个 `Student` 类,其中包含学生的编号、姓名和年龄等属性,同时也提供了对应的访问方法。在 `Main` 类中,通过创建一个 `ArrayList` 对象来存储学生信息,然后通过 `add` 方法向列表中添加一个学生,再通过 `setName` 方法修改学生姓名,最后通过 `remove` 方法删除学生,以及通过 `for` 循环和 `println` 方法查询并输出学生信息。
当然,这只是一个简单的示例,实际情况下,增删改查的操作可能会更为复杂,需要根据具体需求进行调整和优化。
相关推荐
![](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)