线性和二进制搜索Java

时间:2014-10-13 09:07:14

标签: java binary-search

我想生成输出线性搜索和二进制搜索所需的时间。我的代码如下所示,当我运行它时会出现此错误。 “java.lang.ClassCastException:java.lang.String无法强制转换为Employee”

import java.util.*;
import java.util.Collections;
public class H275{public static void main (String[] args){while (JPL.test()){ 
  Employee[] employees = new Employee[1000000];  
  for (int i = 0, numv = employees.length; i < employees.length; numv = employees.length - ++i)
  {String num = Integer.toString(numv);
    switch (i % 3)
    {case 0 : employees[i] = new Secretary("NameA" + num, "America" + num,
                                           1000.0 * numv); 
    break;
      case 1 : employees[i] = new Manager("NameB" + num, "Australia" + num,
                                          2000.0 * numv);  
      break;
      case 2 : employees[i] = new Salesman("NameC" + num,"Brazil" + num,
                                           1500.0 * numv, 200.0 * numv);
    }
  }
  for (Employee e : employees)  
    e.raise();
  for (Employee.SortType s : Employee.SortType.values())
  {employees[0].setSort(s);
    long startTime = System.nanoTime();
    Arrays.sort(employees);
    long endTime = System.nanoTime();

  }
  employees[0].setSort(Employee.SortType.NAMESORT);
  Arrays.sort(employees);
  String name;
  int count = 0;


 Employee e1;
 for (int i = 0, numv = employees.length; i < employees.length; numv = employees.length - ++i)
  {
   String num = Integer.toString(numv);
   switch (i % 3)
    {case 0 : e1 = new Secretary("NameA" + num, "America" + num,
                                           1000.0 * numv); 
    break;
      case 1 : e1 = new Manager("NameB" + num, "Australia" + num,
                                          2000.0 * numv);  
      break;
      case 2 : e1 = new Salesman("NameC" + num,"Brazil" + num,
                                           1500.0 * numv, 200.0 * numv);
    }
  }



  long startTime = System.nanoTime();
  for (int e = 0; e < employees.length; e += 10000)
  {
    name = employees[e].getName();
    count++;
    if(employees[e].getName().equals(name))
    {

      break;
    }
  }
  long endTime = System.nanoTime();

  System.out.println("\nAverage Elapsed LinearSearch time = " + (endTime - startTime) / (1000000.0 * count) + " milliseconds");
  startTime = System.nanoTime();
  for (int e = 0; e < employees.length; e += 10000)


  {
    String name2 = employees[e].getName();

   Arrays.binarySearch(employees,name2); 


    endTime = System.nanoTime();
    break;
  }

  System.out.println("\nAverage Elapsed BinarySearch time = " + (endTime - startTime) / (1000000.0 * count) + " milliseconds");
}}}


abstract class Employee implements Comparable<Employee>
{
  private String name;
  private String address;
  protected double salary;
  public enum SortType{NAMESORT,ADDRESSSORT,JOBSORT,SALARYSORT};
  static private SortType sortType;
  public Employee(String nm, String add, double sal)
  {
   name=nm;
   address=add;
   salary=sal;
  }
  public abstract double raise();

  public String getName()
  {
   return name; 
  }
  public SortType setSort(SortType sortType)
  {
    return sortType;
  }
  public int compareTo(Employee e)
  {
    return 0;
  }
  public String toString()
  {
    return "hello";

  }
}
class Secretary extends Employee
{
 public Secretary(String name, String add, double sal)
 {
  super(name,add,sal); 
 }
 public double raise()
 {
  return salary; 
 } 

}

class Salesman extends Employee
{
  private double commission;
 public Salesman(String name,String address, double salary, double comm)
 {
   super(name,address,salary);
   commission=comm;
 }
 public double raise()
 {
  return salary+commission; 
 }
 public String toString()
 {

   return "test";
 }

}
class Manager extends Employee
{
  public Manager(String name,String address, double salary)
 {
   super(name,address,salary);
 }
  public double raise()
  {
   return salary+(0.05*salary); 

  }

}

1 个答案:

答案 0 :(得分:1)

Arrays.binarySearch(的第二个参数必须是Employee的实例,因为您的数组包含Employees。所以请致电Arrays.binarySearch(employees,employees[e]);