Java 在binarySearch中传递参数的问题

Java 在binarySearch中传递参数的问题,java,binary-search,Java,Binary Search,我有三节课;包含main方法的Movies/tester类。一个以增量方式对数组进行排序的DVDCollection类和一个具有Constructor方法并覆盖compareTo的DVD Comparable类。我们的讲师要求我们使用Movies/tester类main方法来搜索特定导演的作品集。我完全被卡住了,因为我认为我需要传递一个可比较的数组和一个可比较的目标,但指令说我只传递一个字符串参数。我得到了无效的数据类型。任何人都愿意帮助有需要的学生 public class Movies {

我有三节课;包含main方法的Movies/tester类。一个以增量方式对数组进行排序的DVDCollection类和一个具有Constructor方法并覆盖compareTo的DVD Comparable类。我们的讲师要求我们使用Movies/tester类main方法来搜索特定导演的作品集。我完全被卡住了,因为我认为我需要传递一个可比较的数组和一个可比较的目标,但指令说我只传递一个字符串参数。我得到了无效的数据类型。任何人都愿意帮助有需要的学生

public class Movies
{
   public static void main (String[] args)
   {
     Comparable found;


      DVDCollection movies = new DVDCollection();

      movies.addDVD ("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
      movies.addDVD ("District 9", "Neill Blonkamp", 2009, 19.95, false);
      movies.addDVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
      movies.addDVD ("All About Eve", "Joseph Makiewicz", 1950, 17.50, false);
      movies.addDVD ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);

      System.out.println (movies);

      movies.addDVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
      movies.addDVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false);

      System.out.println (movies);

      Comparable target = ("Jon Favreau");
      found = DVD.searchForDVD(target);
        if (found != null)
            System.out.println ("Found: " + index);
        else
            System.out.println ("The director was not found.");


      Comparable target = ("John Smith");
      found = DVD.searchForDVD(target);
      DVD.searchForDVD(target);
        if (found != null)
            System.out.println ("Found: " + index);
        else
            System.out.println ("The director was not found.");

   }
}

 import java.text.NumberFormat;

public class DVDCollection
{
   private DVD[] list;
   private int count; 
   private double totalCost;

   public DVDCollection()
   {
       list = new DVD[100];
       count = 0;
       totalCost = 0;
   }
   public void addDVD (String title, String director, int year, double cost, boolean bluray)
   {
       list[count] = new DVD (title, director, year, cost, bluray);
       for (int index = 1; index < list.length; index++)
       {

           DVD key = list[count];
           int position = count;

           while (position > 0 && key.compareTo(list[position-1]) < 0)
           {
               list[position] = list[position-1];
               position--;
           }
           list[position] = key;

       } 
       totalCost += cost;
       count++;
   }

   public String toString()
   {
      NumberFormat fmt = NumberFormat.getCurrencyInstance();

      String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
      report += "My DVD Collection\n\n";

      report += "Number of DVDs: " + count + "\n";
      report += "Total cost: " + fmt.format(totalCost) + "\n";
      report += "Average cost: " + fmt.format(totalCost/count);

      report += "\n\nDVD List:\n\n";

      for (int dvd = 0; dvd < count; dvd++)
         report += list[dvd].toString() + "\n";

      return report;
   }

} 

import java.text.NumberFormat;
public class DVD implements Comparable
{
   private String title, director;
   private int year;
   private double cost;
   private boolean bluray;

   public DVD (String title, String director, int year, double cost, boolean bluray)
   {
      this.title = title;
      this.director = director;
      this.year = year;
      this.cost = cost;
      this.bluray = bluray;
   }
   public String toString ()
   {
      NumberFormat fmt = NumberFormat.getCurrencyInstance();

      String description;

      description = fmt.format(cost) + "\t" + year + "\t";
      description += title + "\t" + director;

      if (bluray)
         description += "\t" + "Blu-Ray";

      return description;
   }

   public String getDirector ()
   {
      return director;
   }
   public int compareTo (Object list)
   {
      int result;

      String otherDirector = ((DVD)list).getDirector();
      result = director.compareTo(otherDirector);

      return result;
   }

   public static int searchForDVD (String director) 
   {
       int index = 0, min = 0, max = 7, mid=0;
       boolean found = false;
       while (!found && min <= max)
       {
           mid = (min+max) / 2;
           if (director.compareTo(director) == 0)
            return index;
           else 
                if (director.compareTo(director) < 0)
                    max = mid - 1;
                else
                    min = mid + 1;
       }
       if (found)
        return index;
       else 
        return -1;
    }     

}
公共类电影
{
公共静态void main(字符串[]args)
{
可比发现;
DVDCollection电影=新的DVDCollection();
movies.addDVD(“教父”,“弗朗西斯·福特·科波拉”,1972年,24.95,真实版);
movies.addDVD(“第9区”,“尼尔·布朗坎普”,2009年,19.95,假);
movies.addDVD(“钢铁侠”,“乔恩·法夫罗”,2008年,15.95,假);
movies.addDVD(“关于夏娃的一切”,“约瑟夫·马基维茨”,1950年,17.50,假);
movies.addDVD(“矩阵”,“安迪和拉娜·瓦乔夫斯基”,1999年,19.95,真实版);
System.out.println(电影);
movies.addDVD(“钢铁侠2”,“乔恩·法夫罗”,2010年,22.99,假);
movies.addDVD(“卡萨布兰卡”,“迈克尔·柯蒂斯”,1942年,19.95,假);
System.out.println(电影);
可比目标=(“Jon Favreau”);
找到=DVD.searchForDVD(目标);
如果(找到!=null)
System.out.println(“找到:+索引);
其他的
System.out.println(“未找到董事”);
可比目标=(“约翰·史密斯”);
找到=DVD.searchForDVD(目标);
searchForDVD(目标);
如果(找到!=null)
System.out.println(“找到:+索引);
其他的
System.out.println(“未找到董事”);
}
}
导入java.text.NumberFormat;
公共级DVD收藏
{
私人DVD[]列表;
私人整数计数;
私人双重成本;
公共DVD收藏()
{
列表=新DVD[100];
计数=0;
总成本=0;
}
public void addDVD(字符串标题、字符串控制器、整数年、双倍成本、布尔bluray)
{
列表[计数]=新DVD(片名、导演、年份、成本、bluray);
for(int index=1;index0&&key.compareTo(列表[position-1])<0)
{
列表[位置]=列表[位置-1];
位置--;
}
列表[位置]=键;
} 
总成本+=成本;
计数++;
}
公共字符串toString()
{
NumberFormat fmt=NumberFormat.getCurrencyInstance();
字符串报告=“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n”;
报告+=“我的DVD收藏\n\n”;
报告+=“DVD数量:“+count+”\n”;
报表+=“总成本:”+fmt.format(总成本)+“\n”;
报告+=“平均成本:”+fmt.格式(总成本/计数);
报告+=“\n\nDVD列表:\n\n”;
用于(int dvd=0;dvd虽然(!found&&min我希望下面的代码是您所期望的,但请将其作为一个单独的程序尝试并执行

public class Movies
{
   public static DVDCollection movies;
   public static void main (String[] args)
   {
     Comparable found;
     int index;

   movies= new DVDCollection();

  movies.addDVD ("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
  movies.addDVD ("District 9", "Neill Blonkamp", 2009, 19.95, false);
  movies.addDVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
  movies.addDVD ("All About Eve", "Joseph Makiewicz", 1950, 17.50, false);
  movies.addDVD ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);

  System.out.println (movies);

  movies.addDVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
  movies.addDVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false);

  System.out.println (movies);

  Comparable target = ("Jon Favreau");
  System.out.println("Target: "+target.toString());
  found = DVD.searchForDVD(target.toString());
  index=Integer.parseInt(found.toString());

  System.out.println ("Found: " + index);      

  Comparable target1 = ("John Smith");
  System.out.println("Target1: "+target1.toString());
  found = DVD.searchForDVD(target1.toString());
  index=Integer.parseInt(found.toString());

    System.out.println ("Found1: " + index);   
   }
}



  class DVDCollection
   {
      public static DVD[] list;
      private int count; 
     private double totalCost;

    public DVDCollection()
   {
      list = new DVD[100];
     count = 0;
     totalCost = 0;
   }
    public void addDVD (String title, String director, int year, double cost, boolean bluray)
   {
     list[count] = new DVD (title, director, year, cost, bluray);
     for (int index = 1; index < list.length; index++)
     {

       DVD key = list[count];
       int position = count;

       while (position > 0 && key.compareTo(list[position-1]) < 0)
       {
           list[position] = list[position-1];
           position--;
       }
       list[position] = key;

   } 
   totalCost += cost;
   count++;
    }

   @Override
   public String toString()
  {
   NumberFormat fmt = NumberFormat.getCurrencyInstance();

  String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  report += "My DVD Collection\n\n";

  report += "Number of DVDs: " + count + "\n";
  report += "Total cost: " + fmt.format(totalCost) + "\n";
  report += "Average cost: " + fmt.format(totalCost/count);

  report += "\n\nDVD List:\n\n";

  for (int dvd = 0; dvd < count; dvd++)
     report += list[dvd].toString() + "\n";

  return report;
   }

} 

class DVD implements Comparable
{
 private String title, director;
private int year;
private double cost;
private boolean bluray;

 public DVD (String title, String director, int year, double cost, boolean bluray)
 {
  this.title = title;
  this.director = director;
  this.year = year;
  this.cost = cost;
  this.bluray = bluray;
}

 @Override
public String toString ()
{
  NumberFormat fmt = NumberFormat.getCurrencyInstance();

  String description;

  description = fmt.format(cost) + "\t" + year + "\t";
  description += title + "\t" + director;

  if (bluray)
     description += "\t" + "Blu-Ray";

  return description;
   }

  public String getDirector ()
  {
     return director;
 }

 @Override
 public int compareTo (Object list)
 {
  int result;

  String otherDirector = ((DVD)list).getDirector();
  result = director.compareTo(otherDirector);

  return result;
 }

 public static int searchForDVD (String director) 
 {
   boolean flag=false;
   for(int i=0;i<DVDCollection.list.length;i++)
   {
       if(DVDCollection.list[i]!=null)
       {
           System.out.println("Director: "+DVDCollection.list[i].getDirector());
           if(DVDCollection.list[i].getDirector().equals(director))
           {
               flag=true;
               return i;
           }
       }               
   }
   if(!flag)
       return -1;
   return -1;

   }     

 }
公共类电影
{
公共静态dvd收藏电影;
公共静态void main(字符串[]args)
{
可比发现;
整数指数;
电影=新DVD系列();
movies.addDVD(“教父”,“弗朗西斯·福特·科波拉”,1972年,24.95,真实版);
movies.addDVD(“第9区”,“尼尔·布朗坎普”,2009年,19.95,假);
movies.addDVD(“钢铁侠”,“乔恩·法夫罗”,2008年,15.95,假);
movies.addDVD(“关于夏娃的一切”,“约瑟夫·马基维茨”,1950年,17.50,假);
movies.addDVD(“矩阵”,“安迪和拉娜·瓦乔夫斯基”,1999年,19.95,真实版);
System.out.println(电影);
movies.addDVD(“钢铁侠2”,“乔恩·法夫罗”,2010年,22.99,假);
movies.addDVD(“卡萨布兰卡”,“迈克尔·柯蒂斯”,1942年,19.95,假);
System.out.println(电影);
可比目标=(“Jon Favreau”);
System.out.println(“Target:+Target.toString());
found=DVD.searchForDVD(target.toString());
index=Integer.parseInt(find.toString());
System.out.println(“找到:+索引);
可比目标1=(“约翰·史密斯”);
System.out.println(“Target1:+Target1.toString());
found=DVD.searchForDVD(target1.toString());
index=Integer.parseInt(find.toString());
System.out.println(“Found1:+索引”);
}
}
类DVD系列
{
公共静态DVD[]列表;
私人整数计数;
私人双重成本;
公共DVD收藏()
{
列表=新DVD[100];
计数=0;
总成本=0;
}
public void addDVD(字符串标题、字符串控制器、整数年、双倍成本、布尔bluray)