方法、步骤或设计框架: 实验中需要使用记录结构和非记录结构分别实现学籍管理系统。 记录结构:对于记录结构实现方式,就可以考虑使用Java的class来封装每个学生的学籍信息,然后再通过链表链接每一个学生,从而实现对每个学生的记录和学籍信息的获取与修改。 非记录结构:对于非记录结构的实现方式,这里我们考虑使用一个Object数组存储一个学生的信息,然后使用动态数组ArrayList存放各个学生,即可使用ArrayList的get、set等方法进行学生信息的增删改查。 排序的实现:我们只需重写comparator接口的compare方法,即可使用自带的sort实现排序。 数据的导入与导出:这里使用xls文件(excel文件)进行数据的存储。因此我们只需要使用外部的jxl.jar包中的方法即可对excel表格数据进行读入与导出操作。 系统功能设计:为了满足学籍管理系统的正常需求,这里我们设计学生学籍的增删改查、成绩排序、学号排序、数据导入与导出、查看全部信息以及退出系统等十项系统功能。 数据类型与顺序:对于学生学籍信息,我们策划所有数据顺序和对应类型如下:
学生信息 | 数组下标 | 类成员变量 | 数据类型 | 姓名 | 0 | name | String | 年龄 | 1 | age | Int | 性别 | 2 | sex | String | 成绩 | 3 | score | Int | 年级 | 4 | grade | Int | 学号 | 5 | sid | Int |
上面表格中列举信息中,学号用于寻找和识别指定学生。
记录结构实现 ManageLinkedList.java package StudentManage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ManageLinkList {
Scanner scanner = new Scanner(System.in);
LinkedList<Object> ll = new LinkedList<Object>();
public ManageLinkList() {
super();
}
public void menu() {
int option;
while (true) {
System.out.println("\n***************************************");
System.out.println("==============学生学籍管理系统==============");
System.out.println("1.新增学生 2.删除学生");
System.out.println("3.修改学生 4.查找学生");
System.out.println("5.学号排序 6.成绩排序");
System.out.println("7.导入数据 8.导出数据");
System.out.println("9.查看全部 10.退出系统");
System.out.println("输入1-10进行操作:");
option = scanner.nextInt();
switch (option) {
case 1:
stuAdd();
break;
case 2:
stuDelete();
break;
case 3:
stuUpdate();
break;
case 4:
stuRetrieve();
break;
case 5:
stuSortwithSid();
break;
case 6:
stuSortwithScore();
break;
case 7:
ExcelInput();
break;
case 8:
ExcelOutput();
break;
case 9:
stuShowAll();
break;
case 10:
stuExit();
break;
default:
break;
}
}
}
public void stuExit() {
System.out.println("确认退出系统?(y/n)");
if (scanner.next().equals("y")) {
System.out.println("退出系统成功!");
scanner.close();
System.exit(-1);
}
}
public void stuAdd() {
String name, sex;
int score, age, sid, grade;
System.out.println("操作:新增学生");
System.out.println("请输入学生信息:");
System.out.println("姓名:");
name = scanner.next();
System.out.println("年龄:");
age = scanner.nextInt();
System.out.println("性别:");
sex = scanner.next();
System.out.println("成绩:");
score = scanner.nextInt();
System.out.println("年级:");
grade = scanner.nextInt();
System.out.println("学号:");
sid = scanner.nextInt();
Student student = new Student(name, sex, grade, sid, score, age);
ll.add(student);
System.out.println("学生信息新增成功!");
}
public void stuDelete() {
int sid;
boolean ismatched = false;
System.out.println("操作:删除学生");
System.out.println("请输入学号:");
sid = scanner.nextInt();
int i = 0;
// 匹配学生学号
while (i < ll.size()) {
Student student = (Student) ll.get(i);
if (student.getsid() == sid) {
ismatched = true;
// 打印学生名字,再次确认学生信息
System.out.println("确认删除" + student.getname() + "学籍?(y/n)");
if (scanner.next().equals("y")) {
ll.remove(student);
System.out.println("学生信息删除成功!");
} else {
System.out.println("删除操作已取消!");
}
break;
}
i++;
}
if (ismatched == false) {
System.out.println("该学号不存在!请确认输入是否无误!");
}
}
public void stuUpdate() {
String sex;
int score, age, sid, grade;
String massage;
boolean ismatched = false;
System.out.println("操作:修改学生");
System.out.println("请输入学号:");
sid = scanner.nextInt();
int i = 0;
// 匹配学生学号
while (i < ll.size()) {
Student student = (Student) ll.get(i);
// 匹配学生学号
if (student.getsid() == sid) {
ismatched = true;
System.out.println("确认修改" + student.getname() + "学籍?(y/n)");
if (scanner.next().equals("y")) {
System.out.println("请输入学生信息:");
System.out.println("年龄:");
age = scanner.nextInt();
student.setage(age);
System.out.println("性别:");
sex = scanner.next();
student.setsex(sex);
System.out.println("成绩:");
score = scanner.nextInt();
student.setscore(score);
System.out.println("年级:");
grade = scanner.nextInt();
student.setgrade(grade);
// 打印修改后的学生学籍信息
massage = student.getmassage();
System.out.println("学生信息修改成功!");
System.out.println(massage);
} else {
System.out.println("修改操作已取消!");
}
break;
}
i++;
}
if (ismatched == false) {
System.out.println("学号不存在!请确认输入是否无误!");
}
}
public void stuRetrieve() {
int sid;
String massage;
boolean ismatched = false;
System.out.println("操作:查找学生");
System.out.println("请输入学号:");
sid = scanner.nextInt();
int i = 0;
// 匹配学生学号
while (i < ll.size()) {
Student student = (Student) ll.get(i);
// 匹配学生学号
if (student.getsid() == sid) {
ismatched = true;
System.out.println("确认查询" + student.getname() + "学籍?(y/n)");
if (scanner.next().equals("y")) {
// 打印修改后的学生学籍信息
massage = student.getmassage();
System.out.println("学生信息查询结果如下:");
System.out.println(massage);
} else {
System.out.println("查询操作已取消!");
}
break;
}
i++;
}
if (ismatched == false) {
System.out.println("学号不存在!请确认输入是否无误!");
}
}
public void stuShowAll() {
System.out.println("操作:查看全部");
String massage;
int i = 0;
// 匹配学生学号
System.out.println("所有学生学籍信息如下:");
while (i < ll.size()) {
Student student = (Student) ll.get(i);
massage = student.getmassage();
System.out.println(massage);
i++;
}
}
Comparator<Object> SidComparator = new Comparator<Object>() {
public int compare(Object op1, Object op2) {
Student lstu = (Student) op1;
Student rstu = (Student) op2;
// 按姓名排序
if (lstu.getsid() > rstu.getsid())
return 1;
else if (lstu.getsid() < rstu.getsid())
return -1;
else {
return 0;
}
}
};
Comparator<Object> ScoreComparator = new Comparator<Object>() {
public int compare(Object op1, Object op2) {
Student lstu = (Student) op1;
Student rstu = (Student) op2;
// 按姓名排序
if (lstu.getscore() < rstu.getscore())
return 1;
else if (lstu.getscore() > rstu.getscore())
return -1;
else {
return 0;
}
}
};
public void stuSortwithSid() {
String massage;
System.out.println("操作:学号排序");
long startTime = System.nanoTime(); // 获取开始时间
Collections.sort(ll, SidComparator);
long endTime = System.nanoTime(); // 获取结束时间
System.out.println("所有学生学籍信息如下:");
int i = 0;
// 匹配学生学号
while (i < ll.size()) {
Student student = (Student) ll.get(i);
massage = student.getmassage();
System.out.println(massage);
i++;
}
System.out.println("\n记录结构学号排序花费时间: " + (endTime - startTime) + "ns");
}
public void stuSortwithScore() {
String massage;
System.out.println("操作:成绩排序");
long startTime = System.nanoTime(); // 获取开始时间
Collections.sort(ll, ScoreComparator);
long endTime = System.nanoTime(); // 获取结束时间
System.out.println("所有学生学籍信息如下:");
int i = 0;
while (i < ll.size()) {
Student student = (Student) ll.get(i);
massage = student.getmassage();
System.out.println(massage);
i++;
}
System.out.println("\n记录结构成绩排序花费时间: " + (endTime - startTime) + "ns");
}
public void ExcelInput() {
String excelpath;
String name, sex;
int score, age, sid, grade;
System.out.println("操作:导入数据");
System.out.println("请输入导入Excel文件路径:");
excelpath = scanner.next();
File file = new File(excelpath);
try {
// 创建输入流,读取Excel
InputStream is = new FileInputStream(file.getAbsolutePath());
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
// Excel的页签数量
int sheet_size = wb.getNumberOfSheets();
for (int index = 0; index < sheet_size; index++) {
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheet(index);
// sheet.getRows()返回该页的总行数
for (int i = 1; i < sheet.getRows(); i++) {
name = sheet.getCell(0, i).getContents();
age = Integer.parseInt(sheet.getCell(1, i).getContents());
sex = sheet.getCell(2, i).getContents();
score = Integer.parseInt(sheet.getCell(3, i).getContents());
grade = Integer.parseInt(sheet.getCell(4, i).getContents());
sid = Integer.parseInt(sheet.getCell(5, i).getContents());
Student student = new Student(name, sex, grade, sid, score, age);
ll.add(student);
}
System.out.println("学生信息导入成功!");
}
} catch (FileNotFoundException e) {
System.err.println("出现错误:未能找到文件!学生信息导入失败!");
e.printStackTrace();
} catch (BiffException e) {
System.err.println("出现错误:不支持xlsx文件!学生信息导入失败!");
e.printStackTrace();
} catch (NumberFormatException e) {
System.err.println("出现错误:学生数据格式错误!学生信息导入失败!");
e.printStackTrace();
} catch (IOException e) {
System.err.println("出现错误:IO流异常!学生信息导入失败!");
e.printStackTrace();
}
}
public void ExcelOutput() {
String excelpath;
System.out.println("操作:导出数据");
System.out.println("请输入导出Excel文件路径:");
excelpath = scanner.next();
try {
FileOutputStream fos = new FileOutputStream(excelpath);
WritableWorkbook wwb = Workbook.createWorkbook(fos);
// 创建工作表
WritableSheet ws = wwb.createSheet("学生学籍表", 10);
// 设置单元格的文字格式
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setAlignment(Alignment.LEFT);
ws.setColumnView(4, 10);
ws.setColumnView(5, 16);
ws.setColumnView(0, 14);
ws.addCell(new Label(0, 0, "姓名", wcf));
ws.addCell(new Label(1, 0, "年龄", wcf));
ws.addCell(new Label(2, 0, "性别", wcf));
ws.addCell(new Label(3, 0, "成绩", wcf));
ws.addCell(new Label(4, 0, "年级", wcf));
ws.addCell(new Label(5, 0, "学号", wcf));
// 填充数据的内容
// 匹配学生学号
System.out.println("所有学生学籍信息如下:");
int cnt = 1;
while (cnt <= ll.size()) {
Student student = (Student) ll.get(cnt - 1);
ws.addCell(new Label(0, cnt, student.getname(), wcf));
ws.addCell(new Label(1, cnt, String.valueOf(student.getage()), wcf));
ws.addCell(new Label(2, cnt, student.getsex(), wcf));
ws.addCell(new Label(3, cnt, String.valueOf(student.getscore()), wcf));
ws.addCell(new Label(4, cnt, String.valueOf(student.getgrade()), wcf));
ws.addCell(new Label(5, cnt, String.valueOf(student.getsid()), wcf));
cnt++;
if (cnt == 0)
wcf = new WritableCellFormat();
}
wwb.write();
wwb.close();
System.out.println("学生信息导出成功!");
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ManageLinkList mll = new ManageLinkList();
mll.menu();
//ManageArrayList mal = new ManageArrayList();
//mal.menu();
}
}
Student.java package StudentManage;
public class Student {
private String name;
private String sex;
private int grade;
private int sid;
private int score;
private int age;
public Student() {
super();
}
public Student(String name, String sex, int grade, int sid, int score, int age) {
this.name = name;
this.sex = sex;
this.grade = grade;
this.sid = sid;
this.score = score;
this.age = age;
}
public String getname() {
return name;
}
public String getsex() {
return sex;
}
public int getgrade() {
return grade;
}
public int getsid() {
return sid;
}
public int getscore() {
return score;
}
public int getage() {
return age;
}
public void setname(String name) {
this.name = name;
}
public void setsex(String sex) {
this.sex = sex;
}
public void setgrade(int grade) {
this.grade = grade;
}
public void setsid(int sid) {
this.sid = sid;
}
public void setscore(int score) {
this.score = score;
}
public void setage(int age) {
this.age = age;
}
public String getmassage() {
return "姓名:" + name + " 年龄:" + age + " 性别:" + sex + " 成绩:" + score + " 年级:" + grade + " 学号:" + sid;
}
}
非记录结构 package StudentManage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ManageArrayList {
ArrayList<Object> al = new ArrayList<Object>();
Scanner scanner = new Scanner(System.in);
public ManageArrayList() {
super();
}
public void menu() {
int option;
while (true) {
System.out.println("\n***************************************");
System.out.println("==============学生学籍管理系统==============");
System.out.println("1.新增学生 2.删除学生");
System.out.println("3.修改学生 4.查找学生");
System.out.println("5.学号排序 6.成绩排序");
System.out.println("7.导入数据 8.导出数据");
System.out.println("9.查看全部 10.退出系统");
System.out.println("输入1-10进行操作:");
option = scanner.nextInt();
switch (option) {
case 1:
stuArrayAdd();
break;
case 2:
stuArrayDelete();
break;
case 3:
stuArrayUpdate();
break;
case 4:
stuArrayRetrieve();
break;
case 5:
stuArraySortwithSid();
break;
case 6:
stuArraySortwithScore();
break;
case 7:
ArrayExcelInput();
break;
case 8:
ArrayExcelOutput();
break;
case 9:
stuArrayShowAll();
break;
case 10:
stuArrayExit();
break;
default:
break;
}
}
}
public void stuArrayExit() {
System.out.println("确认退出系统?(y/n)");
if (scanner.next().equals("y")) {
System.out.println("退出系统成功!");
scanner.close();
System.exit(-1);
}
}
public void stuArrayAdd() {
String name, sex;
int score, age, sid, grade;
System.out.println("操作:新增学生");
System.out.println("请输入学生信息:");
System.out.println("姓名:");
name = scanner.next();
System.out.println("年龄:");
age = scanner.nextInt();
System.out.println("性别:");
sex = scanner.next();
System.out.println("成绩:");
score = scanner.nextInt();
System.out.println("年级:");
grade = scanner.nextInt();
System.out.println("学号:");
sid = scanner.nextInt();
Object[] student = new Object[6];
student[0] = name;
student[1] = age;
student[2] = sex;
student[3] = score;
student[4] = grade;
student[5] = sid;
al.add(student);
System.out.println("学生信息新增成功!");
}
public void stuArrayDelete() {
int sid;
boolean ismatched = false;
System.out.println("操作:删除学生");
System.out.println("请输入学号:");
sid = scanner.nextInt();
// 匹配学生学号
for (int i = 0; i < al.size(); i++) {
Object[] student = (Object[]) al.get(i);
if ((Long) student[5] == sid) {
ismatched = true;
// 打印学生名字,再次确认学生信息
System.out.println("确认删除" + String.valueOf(student[0]) + "学籍?(y/n)");
if (scanner.next().equals("y")) {
al.remove(i);
System.out.println("学生信息删除成功!");
} else {
System.out.println("删除操作已取消!");
}
break;
}
}
if (ismatched == false) {
System.out.println("该学号不存在!请确认输入是否无误!");
}
}
public void stuArrayUpdate() {
String sex;
int score, age, sid, grade;
String massage;
boolean ismatched = false;
System.out.println("操作:修改学生");
System.out.println("请输入学号:");
sid = scanner.nextInt();
// 匹配学生学号
for (int i = 0; i < al.size(); i++) {
Object[] student = (Object[]) al.get(i);
if ((int) student[5] == sid) {
ismatched = true;
System.out.println("确认修改" + String.valueOf(student[0]) + "学籍?(y/n)");
if (scanner.next().equals("y")) {
System.out.println("请输入学生信息:");
System.out.println("年龄:");
age = scanner.nextInt();
student[1] = age;
System.out.println("性别:");
sex = scanner.next();
student[2] = sex;
System.out.println("成绩:");
score = scanner.nextInt();
student[3] = score;
System.out.println("年级:");
grade = scanner.nextInt();
student[4] = grade;
// 打印修改后的学生学籍信息
massage = "姓名:" + String.valueOf(student[0]) + " 年龄:" + String.valueOf(student[1]) + " 性别:"
+ String.valueOf(student[2]) + " 成绩:" + String.valueOf(student[3]) + " 年级:"
+ String.valueOf(student[4]) + " 学号:" + String.valueOf(student[5]);
al.set(i, student);
System.out.println("学生信息修改成功!");
System.out.println(massage);
} else {
System.out.println("删除操作已取消!");
}
break;
}
}
if (ismatched == false) {
System.out.println("学号不存在!请确认输入是否无误!");
}
}
public void stuArrayRetrieve() {
int sid;
String massage;
boolean ismatched = false;
System.out.println("操作:查找学生");
System.out.println("请输入学号:");
sid = scanner.nextInt();
for (int i = 0; i < al.size(); i++) {
Object[] student = (Object[]) al.get(i);
if ((int) student[5] == sid) {
ismatched = true;
System.out.println("确认查询" + String.valueOf(student[0]) + "学籍?(y/n)");
if (scanner.next().equals("y")) {
// 打印修改后的学生学籍信息
massage = "姓名:" + String.valueOf(student[0]) + " 年龄:" + String.valueOf(student[1]) + " 性别:"
+ String.valueOf(student[2]) + " 成绩:" + String.valueOf(student[3]) + " 年级:"
+ String.valueOf(student[4]) + " 学号:" + String.valueOf(student[5]);
System.out.println("学生信息查询成功!");
System.out.println(massage);
} else {
System.out.println("查询操作已取消!");
}
break;
}
}
if (ismatched == false) {
System.out.println("学号不存在!请确认输入是否无误!");
}
}
public void stuArrayShowAll() {
System.out.println("操作:查看全部");
String massage;
System.out.println("所有学生学籍信息如下:");
for (int i = 0; i < al.size(); i++) {
Object[] student = (Object[]) al.get(i);
massage = "姓名:" + String.valueOf(student[0]) + " 年龄:" + String.valueOf(student[1]) + " 性别:"
+ String.valueOf(student[2]) + " 成绩:" + String.valueOf(student[3]) + " 年级:"
+ String.valueOf(student[4]) + " 学号:" + String.valueOf(student[5]);
System.out.println(massage);
}
}
Comparator<Object> SidComparator = new Comparator<Object>() {
public int compare(Object op1, Object op2) {
Object[] st1 = (Object[]) op1;
Object[] st2 = (Object[]) op2;
// 按姓名排序
if ((int) st1[5] < (int) st2[5])
return -1;
else if ((int) st1[5] > (int) st2[5])
return 1;
else
return 0;
}
};
Comparator<Object> ScoreComparator = new Comparator<Object>() {
public int compare(Object op1, Object op2) {
Object[] st1 = (Object[]) op1;
Object[] st2 = (Object[]) op2;
if ((int) st1[3] < (int) st2[3])
return 1;
else if ((int) st1[3] > (int) st2[3])
return -1;
else
return 0;
}
};
public void stuArraySortwithSid() {
System.out.println("操作:学号排序");
long startTime = System.nanoTime(); // 获取开始时间
Collections.sort(al, SidComparator);
long endTime = System.nanoTime(); // 获取结束时间
String massage;
System.out.println("所有学生学籍信息如下:");
for (int i = 0; i < al.size(); i++) {
Object[] student = (Object[]) al.get(i);
massage = "姓名:" + String.valueOf(student[0]) + " 年龄:" + String.valueOf(student[1]) + " 性别:"
+ String.valueOf(student[2]) + " 成绩:" + String.valueOf(student[3]) + " 年级:"
+ String.valueOf(student[4]) + " 学号:" + String.valueOf(student[5]);
System.out.println(massage);
}
System.out.println("\n非记录结构学号排序花费时间: " + (endTime - startTime) + "ns");
}
public void stuArraySortwithScore() {
System.out.println("操作: 成绩排序");
long startTime = System.nanoTime(); // 获取开始时间
Collections.sort(al, ScoreComparator);
long endTime = System.nanoTime(); // 获取结束时间
String massage;
System.out.println("所有学生学籍信息如下:");
for (int i = 0; i < al.size(); i++) {
Object[] student = (Object[]) al.get(i);
massage = "姓名:" + String.valueOf(student[0]) + " 年龄:" + String.valueOf(student[1]) + " 性别:"
+ String.valueOf(student[2]) + " 成绩:" + String.valueOf(student[3]) + " 年级:"
+ String.valueOf(student[4]) + " 学号:" + String.valueOf(student[5]);
System.out.println(massage);
}
System.out.println("\n非记录结构成绩排序花费时间: " + (endTime - startTime) + "ns");
}
public void ArrayExcelInput() {
String excelpath;
String name, sex;
int score, age, sid, grade;
System.out.println("操作:导入数据");
System.out.println("请输入导入Excel文件路径:");
excelpath = scanner.next();
File file = new File(excelpath);
try {
// 创建输入流,读取Excel
InputStream is = new FileInputStream(file.getAbsolutePath());
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
// Excel的页签数量
int sheet_size = wb.getNumberOfSheets();
for (int index = 0; index < sheet_size; index++) {
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheet(index);
// sheet.getRows()返回该页的总行数
for (int i = 1; i < sheet.getRows(); i++) {
name = sheet.getCell(0, i).getContents();
age = Integer.parseInt(sheet.getCell(1, i).getContents());
sex = sheet.getCell(2, i).getContents();
score = Integer.parseInt(sheet.getCell(3, i).getContents());
grade = Integer.parseInt(sheet.getCell(4, i).getContents());
sid = Integer.parseInt(sheet.getCell(5, i).getContents());
Object[] student = new Object[6];
student[0] = name;
student[1] = age;
student[2] = sex;
student[3] = score;
student[4] = grade;
student[5] = sid;
al.add(student);
}
System.out.println("学生信息导入成功!");
}
} catch (FileNotFoundException e) {
System.err.println("出现错误:未能找到文件!学生信息导入失败!");
e.printStackTrace();
} catch (BiffException e) {
System.err.println("出现错误:不支持xlsx文件!学生信息导入失败!");
e.printStackTrace();
} catch (NumberFormatException e) {
System.err.println("出现错误:学生数据格式错误!学生信息导入失败!");
e.printStackTrace();
} catch (IOException e) {
System.err.println("出现错误:IO流异常!学生信息导入失败!");
e.printStackTrace();
}
}
public void ArrayExcelOutput() {
String excelpath;
System.out.println("操作:导出数据");
System.out.println("请输入导出Excel文件路径:");
excelpath = scanner.next();
try {
FileOutputStream fos = new FileOutputStream(excelpath);
WritableWorkbook wwb = Workbook.createWorkbook(fos);
// 创建工作表
WritableSheet ws = wwb.createSheet("学生学籍表", 10);
// 设置单元格的文字格式
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setAlignment(Alignment.LEFT);
ws.setColumnView(4, 10);
ws.setColumnView(5, 16);
ws.setColumnView(0, 14);
ws.addCell(new Label(0, 0, "姓名", wcf));
ws.addCell(new Label(1, 0, "年龄", wcf));
ws.addCell(new Label(2, 0, "性别", wcf));
ws.addCell(new Label(3, 0, "成绩", wcf));
ws.addCell(new Label(4, 0, "年级", wcf));
ws.addCell(new Label(5, 0, "学号", wcf));
for (int i = 1; i <= al.size(); i++) {
Object[] student = (Object[]) al.get(i - 1);
ws.addCell(new Label(0, i, String.valueOf(student[0]), wcf));
ws.addCell(new Label(1, i, String.valueOf(student[1]), wcf));
ws.addCell(new Label(2, i, String.valueOf(student[2]), wcf));
ws.addCell(new Label(3, i, String.valueOf(student[3]), wcf));
ws.addCell(new Label(4, i, String.valueOf(student[4]), wcf));
ws.addCell(new Label(5, i, String.valueOf(student[5]), wcf));
}
if (al.size() == 0)
wcf = new WritableCellFormat();
wwb.write();
wwb.close();
System.out.println("学生信息导出成功!");
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
简要对比分析 可以发现非记录结构进行排序操作时使用时间更短,效率更高。这主要是因为记录结构如链式记录结构在访问结点成员时,花费时间是要比非记录结构要久的多的,效率也很低。不过这种记录结构如果进行的是增删操作,效率就反而会比数组实现的非记录结构要高。这就是在进行结构设计时根据不同需求所进行选择导致的效率差异。在可读性和可写性上,记录结构是要比不用记录结构的实现要好很多。
|