对象数组的长度是固定不变的,无法适应数据变化的需求,为了解决这个问题,Java提供了另一个容器java.util.ArrayList集合类,让我们可以更便捷的存储和操作对象数据 利用集合类制作的代码详见我的另一篇博文https://blog.csdn.net/qq_26838315/article/details/88183834
ManageStaticArray.java
package cn.chenwei.student;
import java.util.Scanner;
/*
@Author:陈小哥cw
@CreateTime:2019-03-05 13:53
@Description:利用Student对象数组制作简易学生管理系统
*/
public class ManageStaticArray {
//设置一个容量为10的Student对象数组
static Student[] stu = new Student[10];
//统计Student对象数组中非空对象的个数
static int num = 0;
public static void main(String[] args) {
menu();
}
//主方法
public static void menu() {
System.out.println("--------------学生选课管理系统-----------");
System.out.println("1.查询全部学生信息\n2.根据学生编号查询\n3.添加学生\n4.修改学生信息\n5.删除学生\n6.退出");
System.out.println("--------------学生选课管理系统-----------");
System.out.print("请输入序号选择功能:");
Scanner sc = new Scanner(System.in);
switch (sc.nextInt()) {
case 1:
findAll();
break;
case 2:
findId();
break;
case 3:
add();
break;
case 4:
change();
break;
case 5:
delete();
break;
case 6:
System.out.println("欢迎下次使用本系统!");
break;
default:
System.out.println("输入格式不规范!");
menu();
break;
}
}
//查询全部学生信息
public static void findAll() {
System.out.println("\n学生管理系统>>>>>>>>>>>查询所有学生信息");
if (num != 0) {
for (int i = 0; i < num; i++) {
System.out.println("学号为:" + stu[i].getId() + "的学生信息为:\n--------------------\n"
+ "姓名:" + stu[i].getName() + "\n年龄为:" + stu[i].getAge() + "\n性别为:"
+ stu[i].getSex() + "\n--------------------\n\n");
}
} else {
System.out.println("本系统暂时无学生信息!");
}
menu();
}
//根据学生id查询学生信息
public static void findId() {
System.out.println("\n学生管理系统>>>>>>>>>>>根据id查询学生信息");
int i;
if (num != 0) {
int j = 0;
System.out.println("请输入学生id!");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
for (i = 0; i < num; i++) {
if (stu[i].getId() == id) {
j++;
System.out.println("学号为:" + stu[i].getId() + "的学生信息为:\n--------------------\n"
+ "姓名:" + stu[i].getName() + "\n年龄为:" + stu[i].getAge() + "\n性别为:"
+ stu[i].getSex() + "\n--------------------\n\n");
break;
}
}
if (j == 0) {
System.out.println("本系统暂时无该id学生信息!");
}
} else {
System.out.println("本系统暂时无学生信息!");
}
menu();
}
//添加学生信息
public static void add() {
System.out.println("\n学生管理系统>>>>>>>>>>>添加学生信息");
Scanner sc = new Scanner(System.in);
//判断Student对象数组是否已满
if (num < stu.length) {
System.out.println("请输入学生id(整数):");
int id = sc.nextInt();
System.out.println("请输入学生姓名:");
String name = sc.next();
System.out.println("请输入学生年龄(整数):");
int age = sc.nextInt();
System.out.println("请输入学生性别:");
String sex = sc.next();
stu[num] = new Student(id, name, age, sex);
num++;
} else {
System.out.println("学生信息已满!请选择增加容量:");
int addNum = sc.nextInt();
Student[] students = new Student[stu.length + addNum];
for (int i = 0; i < stu.length; i++) {
students[i] = stu[i];
}
stu = students;
System.out.println("当前学生容量为:" + stu.length);
}
menu();
}
//修改学生信息
public static void change() {
System.out.println("\n学生管理系统>>>>>>>>>>>修改学生信息");
if (num != 0) {
int j = 0;
System.out.println("请输入学生id!");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
for (int i = 0; i < num; i++) {
if (stu[i].getId() == id) {
String name;
int age;
String sex;
System.out.println("请输入修改后学生姓名:");
name = sc.next();
System.out.println("请输入修改后学生年龄:");
age = sc.nextInt();
System.out.println("请输入修改后学生性别:");
sex = sc.next();
stu[i] = new Student(id, name, age, sex);
j++;
break;
}
}
if (j == 0) {
System.out.println("本系统暂无该id学生信息!");
}
} else {
System.out.println("本系统暂时无学生信息!");
}
menu();
}
//根据id删除学生信息
public static void delete() {
System.out.println("\n学生管理系统>>>>>>>>>>>删除学生信息");
if (num != 0) {
int j = 0;
System.out.println("请输入学生id!");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
for (int i = 0; i < num; i++) {
if (stu[i].getId() == id) {
int oid = stu[i].getId();
if (i != num - 1) {
for (int k = i; k < num; k++) {
stu[k] = stu[k + 1];
}
}
System.out.println("学号为:" + oid + "的学生信息已删除成功!");
num--;
menu();
break;
}
j++;
}
if (j == 0) {
System.out.println("本系统暂无该id学生信息!");
}
} else {
System.out.println("本系统暂时无学生信息!");
}
menu();
}
}
Student.java
package cn.chenwei.student;
/*
@Author:陈小哥cw
@CreateTime:2019-03-05 13:53
@Description:利用Student对象数组制作简易学生管理系统
@QQ:1322311203
*/
public class Student {
private int id;
private String name;
private int age;
private String sex;
public Student() {
}
public Student(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}