规则:定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。
- 学生的属性:学号,姓名,年龄
- 要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断
- 要求2:添加完毕之后,遍历所有学生信息
- 要求3:通过id删除学生信息:如果存在,则删除,如果不存在,则提示删除失败
- 要求4:删除完毕之后,遍历所有学生信息
- 要求5:查询数组id为"xxxxx"的学生,如果存在,则将他的年龄+1岁
测试类
import java.util.Scanner;
public class FTest {
public static void main(String[] args) {
Student[] stu = new Student[2];
stu[0] = new Student(20221006, "张三", 19);
stu[1] = new Student(20221007, "李四", 20);
Student new_stu = new Student(20221008, "王五", 20);
Scanner sc = new Scanner(System.in);
boolean flag = contains(stu, new_stu.getSno());
if (flag) {
System.out.println("当前id重复,请修改id后再次进行添加!");
} else {
int count = getCount(stu);
if (count == stu.length) {
Student[] newStu = creatNewArr(stu);
newStu[count] = new_stu;
printArr(newStu);
System.out.println("请输入要删除的学生id:");
int id = sc.nextInt();
int index = getIndex(newStu, id);
if (index >= 0) {
newStu[index] = null;
System.out.println("删除成功");
} else {
System.out.println("学生不存在,删除失败!");
}
printArr(newStu);
System.out.println("请输入要查询的学生id:");
int queryId = sc.nextInt();
int query_index = getIndex(newStu, queryId);
if(query_index == -1){
System.out.println("您查询的" + queryId + "不存在");
}else{
int tempAge = newStu[query_index].getAge();
newStu[query_index].setAge(tempAge + 1);
}
printArr(newStu);
} else {
stu[count] = new_stu;
printArr(stu);
System.out.println("请输入要删除的学生id:");
int id = sc.nextInt();
int index = getIndex(stu, id);
if (index >= 0) {
stu[index] = null;
System.out.println("删除成功");
} else {
System.out.println("学生不存在,删除失败!");
}
printArr(stu);
System.out.println("请输入要查询的学生id:");
int queryId = sc.nextInt();
int query_index = getIndex(stu, queryId);
if(query_index == -1){
System.out.println("您查询的" + queryId + "不存在");
}else{
int tempAge = stu[query_index].getAge();
stu[query_index].setAge(tempAge + 1);
}
printArr(stu);
}
}
}
public static boolean contains(Student[] arr, int id) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
if (arr[i].getSno() == id) {
return true;
}
}
}
return false;
}
public static int getCount(Student[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
count++;
}
}
return count;
}
public static Student[] creatNewArr(Student[] arr) {
Student[] newArr = new Student[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
public static void printArr(Student[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
System.out.println("Sno=" + arr[i].getSno() + ", name='" + arr[i].getName() + ", age=" + arr[i].getAge());
}
}
}
public static int getIndex(Student[] arr, int id) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
if (arr[i].getSno() == id) {
return i;
}
}
}
return -1;
}
}
JavaBean类
public class Student{
int Sno;
String name;
int age;
public Student(){}
public Student(int Sno,String name,int age){
this.Sno = Sno;
this.name = name;
this.age = age;
}
public int getSno() {
return Sno;
}
public void setSno(int sno) {
Sno = sno;
}
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;
}
}