用java写一个关于学生的顺序表,包含ID,学生姓名,年龄,爱好
时间: 2024-03-06 08:50:27 浏览: 66
数据结构:定义一个包含学生信息(学号、姓名、成绩)的顺序表_数据结构
5星 · 资源好评率100%
好的,以下是一个简单的示例代码:
```java
public class Student {
private int id;
private String name;
private int age;
private String hobby;
public Student(int id, String name, int age, String hobby) {
this.id = id;
this.name = name;
this.age = age;
this.hobby = hobby;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = 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;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
public class StudentList {
private Student[] students; // 存放学生信息的数组
private int size; // 数组实际长度
public StudentList(int capacity) {
students = new Student[capacity];
size = 0;
}
// 添加学生
public void addStudent(Student student) {
if (size == students.length) {
// 数组已满,需要扩容
Student[] newStudents = new Student[students.length * 2];
System.arraycopy(students, 0, newStudents, 0, students.length);
students = newStudents;
}
students[size++] = student;
}
// 根据ID查找学生
public Student findStudentById(int id) {
for (int i = 0; i < size; i++) {
if (students[i].getId() == id) {
return students[i];
}
}
return null;
}
// 根据姓名查找学生
public Student[] findStudentsByName(String name) {
int count = 0;
for (int i = 0; i < size; i++) {
if (students[i].getName().equals(name)) {
count++;
}
}
if (count == 0) {
return null;
}
Student[] result = new Student[count];
int index = 0;
for (int i = 0; i < size; i++) {
if (students[i].getName().equals(name)) {
result[index++] = students[i];
}
}
return result;
}
// 根据年龄查找学生
public Student[] findStudentsByAge(int age) {
int count = 0;
for (int i = 0; i < size; i++) {
if (students[i].getAge() == age) {
count++;
}
}
if (count == 0) {
return null;
}
Student[] result = new Student[count];
int index = 0;
for (int i = 0; i < size; i++) {
if (students[i].getAge() == age) {
result[index++] = students[i];
}
}
return result;
}
// 根据爱好查找学生
public Student[] findStudentsByHobby(String hobby) {
int count = 0;
for (int i = 0; i < size; i++) {
if (students[i].getHobby().equals(hobby)) {
count++;
}
}
if (count == 0) {
return null;
}
Student[] result = new Student[count];
int index = 0;
for (int i = 0; i < size; i++) {
if (students[i].getHobby().equals(hobby)) {
result[index++] = students[i];
}
}
return result;
}
// 删除学生
public boolean deleteStudent(int id) {
int index = -1;
for (int i = 0; i < size; i++) {
if (students[i].getId() == id) {
index = i;
break;
}
}
if (index == -1) {
return false;
}
// 将要删除的元素后面的元素依次向前移动一位
for (int i = index; i < size - 1; i++) {
students[i] = students[i + 1];
}
students[--size] = null; // 将最后一个元素置为null,便于垃圾回收
return true;
}
public int getSize() {
return size;
}
}
```
使用示例:
```java
public class Test {
public static void main(String[] args) {
StudentList list = new StudentList(5);
list.addStudent(new Student(1, "张三", 18, "篮球"));
list.addStudent(new Student(2, "李四", 19, "足球"));
list.addStudent(new Student(3, "王五", 20, "乒乓球"));
list.addStudent(new Student(4, "赵六", 21, "游泳"));
list.addStudent(new Student(5, "钱七", 22, "跑步"));
// 根据ID查找学生
Student student1 = list.findStudentById(3);
System.out.println(student1.getName()); // 王五
// 根据姓名查找学生(可能有多个)
Student[] students1 = list.findStudentsByName("李四");
for (Student student : students1) {
System.out.println(student.getName()); // 李四
}
// 根据年龄查找学生(可能有多个)
Student[] students2 = list.findStudentsByAge(19);
for (Student student : students2) {
System.out.println(student.getName()); // 李四
}
// 根据爱好查找学生(可能有多个)
Student[] students3 = list.findStudentsByHobby("游泳");
for (Student student : students3) {
System.out.println(student.getName()); // 赵六
}
// 删除学生
list.deleteStudent(2);
System.out.println(list.getSize()); // 4
}
}
```
阅读全文