java吧 关注:1,234,220贴子:12,701,018
  • 9回复贴,共1

创建ArrayList对象时显示CloneNotSupportedException错误

只看楼主收藏回复

class EmpManager{
private ArrayList Emp = null;
public EmpManager(){
Emp = new ArrayList();
}
//添加雇员
public void addEmp(Employee employee){
Emp.add(employee);
}
//根据编号显示信息
public void getInfo(String empNo){
Employee employee = new Employee();
for(int i=0;i<Emp.size();i++){
employee = (Employee) Emp.get(i);
if(employee.equals(empNo)) break;
}
System.out.println("工号为"+empNo+"的员工信息:");
System.out.println("姓名"+employee.getName());
System.out.println("年龄"+employee.getAge());
System.out.println("薪资"+employee.getSal());
System.out.println();
}
//打印所有员工信息
public void getAll(){
for(int i=0;i<Emp.size();i++){
Employee employee = (Employee) Emp.get(i);
System.out.println("工号为"+employee.getEmpNo()+"的员工信息:");
System.out.println("姓名"+employee.getName());
System.out.println("年龄"+employee.getAge());
System.out.println("薪资"+employee.getSal());
System.out.println();
}
}
//修改薪水
public void updateSal(String empNo,float sal){
Employee employee = new Employee();
for(int i=0;i<Emp.size();i++){
employee = (Employee) Emp.get(i);
if(employee.equals(empNo)) break;
}
employee.updateSal(sal);
}
//删除员工
public void remove(String empNo){
Employee employee = new Employee();
for(int i=0;i<Emp.size();i++){
employee = (Employee) Emp.get(i);
if(employee.equals(empNo)) break;
}
Emp.remove(employee);
}
//按工资排序
public void sortOnSal(){
ArrayList temp = new ArrayList();
Employee employee = new Employee();
float temp2 ;
employee = (Employee) Emp.get(0);
temp.add(employee);
float sal;
for(int i=1;i<Emp.size();i++){
employee = (Employee) Emp.get(i);
sal = employee.getSal();
for(int j=0;j<temp.size();j++){
temp2 = ((Employee) temp.get(j)).getSal();
if(sal>temp2){
temp.add(j, employee);
break;
}
if(j == temp.size()-1){
temp.add(employee);
}
}
}
for(int i=1;i<=temp.size();i++){
employee = (Employee) temp.get(i-1);
System.out.println(i+": 工号:"+employee.getEmpNo()+" 姓名"+employee.getName()+"年龄"+employee.getAge()+"薪资"+employee.getSal());
}
}
}
错误信息
java.lang.CloneNotSupportedException: interfaces.Employee
at java.lang.Object.clone(Native Method)
at interfaces.Employee.clone(Employee.java:19)
at interfaces.interfaceTestEmployees.main(interfaceTestEmployees.java:13)
我看韩顺平的视频的时候虽然他的代码也有提示叹号,但是却没有出错。
代码质量求轻喷0.0


IP属地:湖北1楼2017-01-17 00:17回复
    求解答,


    IP属地:湖北来自Android客户端2楼2017-01-17 09:26
    回复
      远程帮你看看 具体环境搞不清楚
      895686843


      IP属地:江苏3楼2017-01-17 09:28
      收起回复
        你怎么不把main方法的代码发上来


        4楼2017-01-17 11:02
        回复
          public class ArrayListTest {
          public static void mian (String[] args) {
          EmpManager empManager = new EmpManager();
          Employee em1 = new Employee("001", "张三", 21, 120000);
          Employee em2 = new Employee("002", "赵四", 21, 150000);
          Employee em3 = new Employee("003", "王五", 21, 170000);
          Employee em4 = new Employee("004", "刘能", 24, 100000);
          empManager.addEmp(em1);
          empManager.addEmp(em2);
          empManager.addEmp(em3);
          empManager.addEmp(em4);
          empManager.sortOnSal();
          }
          }
          @zd19931019


          IP属地:湖北5楼2017-01-17 11:44
          收起回复
            这就没报错了吗?


            来自iPhone客户端6楼2017-01-17 11:48
            收起回复