package ArrayObject;
import java.util.Arrays;
public class StudentMain {
public static void main(String[] args) {
// 初始化学生数组
Student[] arr = new Student[3];
Student s1 = new Student(2020311, "khr", 23);
Student s2 = new Student(2020312, "gsy", 24);
Student s3 = new Student(2020313, "gsj", 25);
arr[0] = s1;
arr[1] = s2;
arr[2] = s3;
// 添加学生并打印数组
Student s4 = new Student(2020314, "zs", 26);
arr = addStudent(arr, s4);
printArr(arr);
// 再添加一个学生并打印数组
Student s5 = new Student(2020315, "xcxzs", 26);
arr = addStudent(arr, s5);
printArr(arr);
// 删除一个学生并打印数组
arr = deleteStudent(arr, 2020315);
printArr(arr);
}
// 添加学生到数组中
public static Student[] addStudent(Student[] arr, Student s) {
// 检查是否已存在相同ID的学生
boolean flag = contain(arr, s.getId());
if (flag) {
System.out.println("当前id重复,请重新修改id");
return arr; // 如果ID重复,返回原数组
} else {
System.out.println("成功添加学号为" + s.getId() + "的学生");
int count = getCount(arr);
// 如果数组已满,扩展数组
if (count == arr.length) {
Student[] newArr = getArr(arr);
newArr[newArr.length - 1] = s;
return newArr;
} else {
arr[count] = s; // 在数组中添加新学生
return arr;
}
}
}
// 从数组中删除学生
public static Student[] deleteStudent(Student[] arr, int id) {
boolean flag = false;
int index = 0;
// 查找要删除的学生
for (int i = 0; i < arr.length; i++) {
if (arr[i].getId() == id) {
flag = true;
index = i;
break;
}
}
// 如果找到了学生,删除之
if (flag) {
System.out.println("成功删除学号为" + id + "的学生");
Student[] newArr = new Student[arr.length - 1];
int j = 0;
// 复制原数组中的其他学生到新数组
for (int i = 0; i < arr.length; i++) {
if (i != index) {
newArr[j++] = arr[i];
}
}
return newArr;
} else {
System.out.println("未找到指定id的学生");
return arr; // 如果未找到学生,返回原数组
}
}
// 打印学生数组
public static void printArr(Student[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
System.out.println("id:" + arr[i].getId() + ", 姓名:" + arr[i].getName() + ",年龄:" + arr[i].getAge());
}
}
}
// 扩展数组
public static Student[] getArr(Student[] arr) {
Student[] newArr = new Student[arr.length + 1];
System.arraycopy(arr, 0, newArr, 0, arr.length);
return newArr;
}
// 计算数组中非空元素的个数
public static int getCount(Student[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
count++;
}
}
return count;
}
// 检查数组中是否存在具有指定ID的学生
public static boolean contain(Student[] arr, int id) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null && arr[i].getId() == id) {
return true;
}
}
return false;
}
}
package ArrayObject;
public class Student {
private int id;
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
/**
* 获取
* @return id
*/
public int getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(int id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{id = " + id + ", name = " + name + ", age = " + age + "}";
}
}