你好 问题别人已经说清楚了
建议排序的做法是
你的Employee对象实现Compare接口 重写比较方法
再调用Collection的sort方法进行排序
如果不会写 把源码贴出来 我帮你改
j是i+1,那它最后的边界应该比i的边界还小一才对
i的范围才到 newList.size()-1 ;j范围到newList.size();肯定不行
可用stream流进行排序
第二个for循环改成这样
j=i;j<newList.size()-1;j++
降序
newList.stream().sorted(Comparator.comparing(Employee::getGongzi).reversed());
升序
newList.stream().sorted(Comparator.comparing(Employee::getGongzi));
排序调库啊,手写要出错
package application;
public class date {
public double years;
public double months;
public double days;
}package application;
import java.io.Serializable;
public class person implements Serializable {
public String name;
public String sex;
public String id;
public String birth;
public void setName(String n) {
this.name = n;
}
public void setSex(String n) {
this.sex = n;
}
public person(String n, String s, String birth,String id) {
this.name = n;
this.sex = s;
this.birth=birth;
this.id=id;
}
public String getSex() {
return sex;
}
public String getName() {
return name;
}
public String getBirth(){
return birth;
}
public void setBirth(String n){
this.birth=n;
}
public String getId(){
return id;
}
public void setId(String n){
this.id=n;
}
}package application;
import java.io.Serializable;
public class Employee extends person implements Serializable {
public String gonghao;
public String phone;
public String keshi;
public int gongzi;
public Employee(String name, String sex, String id, String birth, String gonghao, String phone, String keshi,int gongzi)
{
super(name,sex,id,birth);
this.gonghao=gonghao;
this.phone=phone;
this.keshi=keshi;
this.gongzi=gongzi;
}
public int getGongzi() {
return gongzi;
}
public String getGonghao() {
return gonghao;
}
public String getKeshi() {
return keshi;
}
public String getName() {
return name;
}
public String toString() {
return "姓名:" + getName() + ",性别:" + getSex() + ",身份证号码:" + getId() + ", \n出生日期:" + getBirth() + ",工号:" + gonghao
+ ",电话" +phone + ", \n科室:" + keshi + ",工资:" +gongzi;
}
}
package application;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class test {
static Scanner sc=new Scanner(System.in);
static File fp=new File("职员工资信息.txt");
static List<Employee>list=new ArrayList<Employee>();
private static void duqu() throws FileNotFoundException,IOException,ClassNotFoundException{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(fp));
list=(List<Employee>)ois.readObject();
ois.close();
}
public static void xieru()throws FileNotFoundException,IOException,ClassNotFoundException{
FileOutputStream os = new FileOutputStream(fp);
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(list);
oos.flush();
oos.close();
}
public static void menu(){
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("**********0.退出系统**************");
System.out.println("****请选择功能,并输入对应序号********");
}
public static void main(String[] args) throws IOException,ClassNotFoundException{
int selet;
while(true){
if(!fp.exists()){
fp.createNewFile();
}else{
duqu();
}
menu();
selet=sc.nextInt();
switch(selet){
case 1:
shuru();
break;
case 2:
chaxun();
break;
case 3:
paixu();
break;
case 4:
xiugai();
break;
case 5:
shanchu();
break;
case 0:
System.exit(0);
}
}
}
private static void shanchu() throws IOException,ClassNotFoundException{
System.out.println("请输入将删除的工号");
String a=sc.next();
Employee m=null;
for (Employee e : list) {
if (a.equals(e.getGonghao())) {
m=e;
}
}
if (m!= null) {
System.out.println("是否确定删除?1.确定 2.取消");
int x=sc.nextInt();
if(x==1){
list.remove(m);
xieru();
System.out.println("已删除");
return;}
else{
return;
}
}
}
private static void xiugai() throws IOException,ClassNotFoundException{
System.out.println("请输入将修改的工号");
String a=sc.next();
int i=0;
for(Employee e:list){
if(a.equals(e.getGonghao())){
System.out.println(e);
System.out.println("请重新输入姓名,性别,身份证号码,出生日期,工号,电话,所在科室,工资");
String name=sc.next();
String sex=sc.next();
String id=sc.next();
String birth=sc.next();
String gonghao=sc.next();
String phone=sc.next();
String keshi=sc.next();
int gongzi=sc.nextInt();
Employee m=new Employee(name,sex,id,birth,gonghao,phone,keshi,gongzi);
list.set(i, m);
xieru();
System.out.println("修改完成");
return;
}else{
System.out.println("输入工号错误!");
}
i++;
}
}
private static void paixu() throws IOException,ClassNotFoundException{
System.out.println("按职员工资大小排序:");
List<Employee>newList=list;
for(int i=0;i<newList.size()-1;i++){
for(int j=i+1;j<newList.size();j++){
Collections.swap(list,newList.get(i).getGongzi(), newList.get(j).getGongzi());
}
for(Employee e:newList){
System.out.println(e);
}
}
}
public static void chaxun() throws IOException,ClassNotFoundException{
System.out.println("请选择:");
System.out.println("1.根据工号查询职工信息\n2.根据姓名和科室查询职工信息\n3.分科室进行工资统计,计算各科室的平均工资");
switch(sc.nextInt()){
case 1:
String a=sc.next();
for(Employee e:list){
if(a.equals(e.getGonghao())){
System.out.println(e.toString());
}
}
break;
case 2:
String a1=sc.next();
String a2=sc.next();
for(Employee e:list){
if(a1.equals(e.getName())||a2.equals(e.getKeshi())){
System.out.println(e.toString());
}
}
break;
case 3:
double []p=new double[50];
int i=0;
System.out.println("请输入要计算的科室");
String a3=sc.next();
for(Employee e:list){
if(a3.equals(e.getKeshi())){
p[i]=e.getGongzi();
System.out.println(p[0]+=p[i]);
i++;
System.out.println("该科室平均工资为:"+(p[0]+=p[i])/(i+1));
}
}
}
}
private static void shuru() throws IOException,ClassNotFoundException{
System.out.println("请输入姓名,性别,身份证号码,生日,工号,电话,所在科室,工资");
String name=sc.next();
String sex=sc.next();
String id=sc.next();
String birth=sc.next();
String gonghao=sc.next();
String phone=sc.next();
String keshi=sc.next();
int gongzi=sc.nextInt();
Employee employee=new Employee(name,sex,id,birth,gonghao,phone,keshi,gongzi);
list.add(employee);
xieru();
System.out.println("已保存成功");
}
}