输入员工信息,按员工的职位和部门排序
运行结果:
本实验主要实现自定义list排序,jtable显示
list有自己的completeTo方法,为了实现自己的排序必须覆盖此方法
public int compareTo(Employee o)
{
Employee other=(Employee)o;
if (this.gangwei.compareTo(other.getGangwei())<0) return 1;
if (this.gangwei.compareTo(other.getGangwei())>0) return -1;
// if (this.gangwei.compareTo(other.getGangwei())==0) return -1;
if (this.zhiwei.compareTo(other.getZhiwei())>0) return 1;
if (this.zhiwei.compareTo(other.getZhiwei())<0) return -1;
if (this.zhiwei.compareTo(other.getZhiwei())==0) return -1;
// if (this.salary>other.salary) return 1;
// if (this.salary<other.salary) return -1;
// if (this.salary==other.salary) return -1;
return 0;
}
先按岗位排序,再按职位排序
全部程序主要有个employee类,里面是员工信息
package test2;
import java.util.*;
import java.util.List;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class test2
{
/**
* @param args
*/
public static void main(String[] args)
{
MyFrame frame=new MyFrame();
// TODO Auto-generated method stub
}
}
class MyFrame extends JFrame
{
Set<Employee> Emp=new TreeSet<Employee>();
private JTable table;
private DefaultTableModel model;
public MyFrame()
{
Emp.add(new Employee("S0102","教师","助教","张飞","1980-1-11", 4000));
Emp.add(new Employee("S3102", "教师","讲师","刘备","1982-11-1", 5000));
Emp.add(new Employee("X1102", "行政","科员","赵云","1988-12-1", 4000));
Emp.add(new Employee("X0302","行政","主任科员","关羽","1985-1-16", 5000));
Emp.add(new Employee("S2102","教师","教授","马超","1966-1-11", 8000));
Emp.add(new Employee("S1202", "教师","副教授","黄忠","1970-11-1", 6000));
Emp.add(new Employee("X3102", "行政","主任行政","曹操","1968-12-1", 6000));
Emp.add(new Employee("S0322","教师","讲师","诸葛亮","1975-1-16", 5500));
Emp.add(new Employee("S3102","教师","助教","张辽","1970-1-1", 4500));
Emp.add(new Employee("S2302", "教师","讲师","貂蝉","1972-11-1", 5500));
Emp.add(new Employee("X1302", "行政","科员","吕布","1978-12-31", 4600));
Emp.add(new Employee("X0602","行政","主任科员","司马懿","1977-10-16", 5400));
Emp.add(new Employee("S6102","教师","教授","荀彧","1969-1-11", 7600));
Emp.add(new Employee("S1262", "教师","副教授","郭嘉","1972-11-21", 6200));
Emp.add(new Employee("X7102", "行政","主任行政","黄盖","1967-12-1", 5400));
Emp.add(new Employee("S0622","教师","讲师","周瑜","1971-11-16", 5600));
this.setTitle("学校职工信息");
this.setSize(600,500);
this.setLocation(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
model=new DefaultTableModel();
model.addColumn("工号");
model.addColumn("岗位");
model.addColumn("职位");
model.addColumn("姓名");
model.addColumn("生日");
model.addColumn("薪水");
String[][] strings=new String[Emp.size()][6];
Iterator<Employee> it = Emp.iterator();
int i=0;
while(it.hasNext()){
Employee e = it.next();
strings[i][0]=e.getNumber();
strings[i][1]=e.getGangwei();
strings[i][2]=e.getZhiwei();
strings[i][3]=e.getName();
strings[i][4]=e.getBirsday();
strings[i][5]=String.valueOf(e.getSalary());
model.addRow(strings[i]);
i++;
}
//model.addRow(r1);
table=new JTable(model);
JPanel jP=new JPanel();
jP.add(new JScrollPane(table),BorderLayout.CENTER);
add(jP);
}
}
class Employee implements Comparable<Employee>
{
private String number;
private String gangwei;
private String zhiwei;
private String name;
private String birsday;
private int salary;
private int value;
public int getValue()
{
return value;
}
public Employee(String number,String gangwei,String zhiwei,String name,String birsday,int salary)
{
this.number=number;
this.gangwei=gangwei;
this.zhiwei=zhiwei;
this.name=name;
this.birsday=birsday;
this.salary=salary;
}
public String getNumber()
{
return number;
}
public String getGangwei()
{
return gangwei;
}
public String getZhiwei()
{
return zhiwei;
}
public String getName()
{
return name;
}
public String getBirsday()
{
return birsday;
}
public int getSalary()
{
return salary;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof Employee))
return false;
final Employee other = (Employee) obj;
if (this.gangwei.equals(other.getGangwei()) && this.zhiwei == other.getZhiwei())
return true;
else
return false;
}
//自定义比较排序
@Override
public int compareTo(Employee o)
{
Employee other=(Employee)o;
if (this.gangwei.compareTo(other.getGangwei())<0) return 1;
if (this.gangwei.compareTo(other.getGangwei())>0) return -1;
// if (this.gangwei.compareTo(other.getGangwei())==0) return -1;
if (this.zhiwei.compareTo(other.getZhiwei())>0) return 1;
if (this.zhiwei.compareTo(other.getZhiwei())<0) return -1;
if (this.zhiwei.compareTo(other.getZhiwei())==0) return -1;
// if (this.salary>other.salary) return 1;
// if (this.salary<other.salary) return -1;
// if (this.salary==other.salary) return -1;
return 0;
}
//写了equal就得重写hashcode
public int hashCode()
{
int result;
result=(gangwei==null?0:gangwei.hashCode());
result=(zhiwei==null?0:zhiwei.hashCode());
return result;
}
}