Java二維數組實作學生管理系統
一、預備知識
1.二維數組的建立
1) DataType [][] name=new DataType[10][];
2) DataType [][] name=new DataType[9][5];
3) DataType [][] name= {};
2.二維數組的初始化
1) DataType [][] name= {{1,2,3,4},{1,2,3},{}};
2) name[0]=new DataType[5];
name[1]=new DataType[]{4,5,6,7};
3) name[0][0]=10;
name[5][2]=6;
3.二維數組的周遊
通過雙層循環周遊
4.需要注意的
DataType [][] name=new DataType[9][5];
當執行name[2]=null後該空間不能被使用
二、系統需求
通過控制台列印如下資訊:
-------------------------學生管理系統------------------------------
1 添加學生
2 查詢學生
3 修改學生
4 删除學生
5 查詢所有學生
6 退出
請輸入編号!
要求:
1.該系統用數組的形式實作,數組長度為10
2.學生資訊有:id name age,在系統初始化時有一條資料:10001 張三 18
3.添加學生:向系統中添加學生,并顯示添加結果
4.查詢學生 :查找學生資訊并顯示查詢結果
5.修改學生 :修改學生資訊
6.删除學生 :删除學生資訊
7.查詢所有學生: 列印所有學生的資訊
8.退出:退出系統
三、代碼實作
1.通過二維數組實作
MyStudent.java
StudentTool.java
import java.util.Scanner;
public class MyStudent {
Scanner in = new Scanner(System.in);
StudentTool studentTool = new StudentTool();
public static void main(String[] args) {
new MyStudent().menu();
}
void showMenu(){
System.out.println("=========學生管理系統======== ");
System.out.println("1 添加學生");
System.out.println("2 查詢學生");
System.out.println("3 修改學生");
System.out.println("4 删除學生");
System.out.println("5 查詢所有學生");
System.out.println("6 退出");
System.out.println("請輸入編号!");
}
public void menu() {
while (true) {
showMenu();
switch (in.next()) {
case "1":
System.out.println("請輸入姓名");
String name= in.next();
System.out.println("請輸入年齡");
String age= in.next();
String []stu={"",name,age};
if(studentTool.addStudent(stu)){
System.out.println("添加成功");
}else {
System.out.println("添加失敗!!");
}
break;
case "2":
System.out.print("請輸入學生ID:");
String id=in.next();
show(studentTool.searchStudentByID(id));
break;
case "3":
System.out.println("請輸入需要修改學生的ID");
String[] searchResult=studentTool.searchStudentByID(in.next()+"");
if(searchResult!=null){
System.out.println("請輸入name");
searchResult[1]=in.next();
System.out.println("請輸入age");
searchResult[2]=in.next();
if(studentTool.updataStudent(searchResult)){
System.out.println("修改成功");
}else{
System.out.println("修改失敗");
}
}else {
System.out.println("學生不存在");
}
break;
case "4":
System.out.println("請輸入要删除的學生的ID");
if(studentTool.deleteStudentByID(in.next())) {
System.out.println("删除成功");
}
else {
System.out.println("删除失敗");
}
break;
case "5":
String[][] stus= studentTool.searchAll();
for (int i = 0; i < stus.length; i++) {
show(stus[i]);
}
break;
case "6":
System.exit(0);
break;
default:
System.out.println("輸入有誤請重新輸入!!!");
}
}
}
void show(String[] stu) {
try {
System.out.println("ID:" + stu[0] + "\tname:" + stu[1] + "\tage:" + stu[2]);
} catch (Exception e) {
System.out.println("學生不存在");
}
}
}
public class StudentTool {
String[][] stuDB;
public StudentTool() {
stuDB = new String[5][3];
stuDB[0] = new String[] { "10001", "張三", "18" };
}
public boolean addStudent(String[] stu) {
if(isEmpty()) {
stu[0]="10001";
stuDB[0]=stu;
return true;
}
if (!this.isDBFlow()) {
int index = this.getReallySize();
stu[0] =(Integer.parseInt(stuDB[index-1][0])+1)+"";
stuDB[index] = stu;
return true;
}
return false;
}
public String[] searchStudentByID(String id) {
/**
* 判斷數組是否為空 如果為空就直接傳回
*/
if (this.isEmpty()) {
return null;
}
int length = this.getReallySize();
for (int i = 0; i < length; i++) {
if (this.stuDB[i][0].equals(id)) {
return this.stuDB[i];
}
}
return null;
}
public String[][] searchAll() {
return this.stuDB;
}
/**
* 修改學生資訊
*
* @param id
* @return true:修改成功 false:修改失敗
*/
public boolean updataStudent(String[] stu) {
String[] thisStu = this.searchStudentByID(stu[0]);
if (thisStu != null) {
thisStu[1] = stu[1];
thisStu[2] = stu[2];
return true;
} else {
System.out.println("學生不存在");
return false;
}
}
public boolean deleteStudentByID(String id) {
String[] stu = this.searchStudentByID(id);
int stuLocation = -1;
int size = getReallySize();
if (stu != null) {
for (int i = 0; i < this.getReallySize(); i++) {
if (stuDB[i][0].equals(id)) {
stuLocation = i;
break;
}
}
for (int i = stuLocation, j = size-1; i < j; i++) {
stuDB[i] = stuDB[i + 1];
}
/**
* **********************************************bug**********************************************
* stuDB[size-1] = null;
* **********************************************bug**********************************************
* ********************************二維數組不能直接将一行置為null,隻有将其每個最小空間置為空*************************************
**/
for (int i = 0; i < stuDB[size-1].length; i++) {
stuDB[size-1][i] = null;
}
return true;
} else {
System.out.println("學生不存在,删除失敗");
return false;
}
}
/**
* 判斷數組是否已經滿了
*
* @return true:數組已經滿 false:數組未滿
*/
public boolean isDBFlow() {
int length = stuDB.length;
return stuDB[length - 1][0] != null;
}
/**
* 擷取數組的真實長度
*
* @return
*/
private int getReallySize() {
int i=0 ;
if(isEmpty()) {
return 0;
}
if (isDBFlow()) {
return stuDB.length;
}
int length = stuDB.length;
for (; i < length; i++) {
if (stuDB[i][0] == null) {
break;
}
}
return i;
}
/**
* 判斷數組是否為空
*
* @return true:空 false:非空
*/
private boolean isEmpty() {
return stuDB[0][0] == null;
}
}
2.通過對象數組實作
Main.java
Student.java
StudentUtil.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main().menu();
}
Scanner in=new Scanner(System.in);
void menu() {
StudentUtil studentUtil = new StudentUtil(new Student[10]);
Student student;
String id;
while (true) {
showMenu();
switch (in.next()) {
case "1":
student = new Student();
System.out.print("請輸入姓名:");
student.setName(in.next());
System.out.print("請輸入年齡:");
student.setAge(in.next());
if (studentUtil.add(student)) {
System.out.println("添加成功!");
} else {
System.out.println("添加失敗");
}
break;
case "2":
System.out.print("請輸入學生ID:");
show(studentUtil.searchSingletonById(in.next()));
break;
case "3":
System.out.print("請輸入需要修改學生的ID:");
id=in.next();
if(studentUtil.isExist(id)) {
student = new Student();
student.setId(id);
System.out.print("請輸入姓名:");
student.setName(in.next());
System.out.print("請輸入年齡:");
student.setAge(in.next());
System.out.println(studentUtil.updata(student)?"修改成功!":"修改失敗!2");
}else {
System.out.println("修改失敗!");
}
break;
case "4":
System.out.print("請輸入需要删除學生的ID:");
id=in.next();
if(studentUtil.isExist(id)) {
System.out.println(studentUtil.delete(id)?"删除成功!":"删除失敗!");
}else {
System.out.println("學生不存在,删除失敗!");
}
break;
case "6":
System.exit(0);
break;
case "5":
Student[] students=studentUtil.searchAll();
for (int i = 0; i < students.length; i++) {
show(students[i]);
}
break;
default:
System.out.println("指令不正确,請重新輸入");
break;
}
}
}
void showMenu(){
System.out.println("=========學生管理系統======== ");
System.out.println("1 添加學生");
System.out.println("2 查詢學生");
System.out.println("3 修改學生");
System.out.println("4 删除學生");
System.out.println("5 查詢所有學生");
System.out.println("6 退出");
System.out.println("請輸入編号!");
}
void show(Student stu){
try{
System.out.println("ID:"+stu.getId()+"\tname:"+stu.getName()+"\tage:"+stu.getAge());
}catch (Exception e) {
System.out.println("學生不存在");
// TODO: handle exception
}
}
}
public class Student {
private String id;
private String name;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public class StudentUtil {
private Student[] students;
public StudentUtil (Student[] students) {
this.students=students;
}
public boolean add(Student student) {
if(isEmpty()) {
student.setId("10001");
this.students[0] = student;
return true;
}
if (!isFull()) {
student.setId((Integer.parseInt(this.students[getReallySize()-1].getId())+1)+"");
this.students[getReallySize()] = student;
return true;
}
return false;
}
public boolean delete(String id) {
for (int i = 0; i < getReallySize(); i++) {
if(this.students[i].getId().equals(id)) {
for (int j = i; j < getReallySize()-1; j++) {
this.students[j]=this.students[j+1];
}
this.students[getReallySize()-1]=null;
return true;
}
}
return false;
}
public boolean updata(Student student) {
if (this.isEmpty()) {
return false;
}
for (int i = 0; i < getReallySize(); i++) {
if (students[i].getId().equals(student.getId())) {
students[i].setAge(student.getAge());
students[i].setName(student.getName());
return true;
}
}
return false;
}
public Student searchSingletonById(String id) {
if(this.isEmpty()) {
return null;
}
for (int i = 0; i < getReallySize(); i++) {
if (students[i].getId().equals(id)) {
return students[i];
}
}
return null;
}
public Student[] searchAll() {
return this.students;
}
/**
* 判斷數組是否為空
* 看第一個元素是否為空
* @return
* true:空數組
* false:非空數組
*/
private boolean isEmpty() {
return students[0]==null;
}
/**
* 判斷數組是否滿了
* 看最後一個元素是否不為空
* @return
* true:滿了
* false:未滿
*/
private boolean isFull() {
return students[students.length-1]!=null;
}
/**
* 判斷是否存在該對象中的資料
* @param t
* @return
* true:存在
* false:不存在
*/
public boolean isExist(String id) {
//此處不能用周遊器
// for (Student item : this.students) {
// if(item.getId().equals(id)) {
// return true;
// }
// }
// return false;
for (int i = 0; i < getReallySize(); i++) {
if (students[i].getId().equals(id)) {
return true;
}
}
return false;
}
public int getReallySize() {
int i=0 ;
if(isEmpty()) {
return 0;
}
if(isFull()) {
return students.length;
}
for (; i < students.length; i++) {
if(students[i]==null) {
break;
}
}
return i;
}
}