For-Each循环不兼容的类型

时间:2012-03-29 13:58:16

标签: java database-design foreach incompatibletypeerror

我正在开发一个为电影创建虚拟数据库的项目。我有两个类:MovieEntry(用于单独的电影条目)和MovieDatabase(包含数据库并允许添加的较大类等)我收到一些错误,第一个是在searchTitle方法中它说数据库是不兼容的类型。谁能告诉我如何为每个循环做这些?我读了这本书,我认为ArrayList应该去那里,但显然不是。

**import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class MovieDatabase
{
   private ArrayList<MovieEntry> Database = new ArrayList<MovieEntry>();
   public MovieDatabase(){
       ArrayList<MovieDatabase> Database = new ArrayList<MovieDatabase>(0);
    }

   public int countTitles() throws IOException{
   Scanner fileScan;
   fileScan = new Scanner (new File("movies.txt"));
   int count = 0;
   String movieCount;
   while(fileScan.hasNext()){
       movieCount = fileScan.nextLine();
       count++;
    }
   return count;
}

   public void addMovie(MovieEntry m){
       Database.add(m);
    }

   public ArrayList<MovieEntry> searchTitle(String substring){
       for (String title : Database)
          System.out.println(title);
          return null;
    }

   public ArrayList<MovieEntry> searchGenre(String substring){
       for (String genre : Database)
          System.out.println(genre);
          return null;
    }

   public ArrayList<MovieEntry> searchDirector (String str){
       for (String director : Database)
          System.out.println(director);
          return null;
    }

   public ArrayList<MovieEntry> searchYear (int yr){
       ArrayList <String> yearMatches = new ArrayList<String>();
       for (String s : Database)
       s.getYear();
        if(yearMatches.contains(y) == false){
           yearMatches.add(y);
        }
       return yearMatches;
    }

   public ArrayList<MovieEntry> searchYear(int from, int to){
       ArrayList <String> Matches = new ArrayList<String>();
       for(Student s : movies);
          Matches.add();
       return Matches;
    }

   public void readMovieData(String movies){
       String info;
       try{
           Scanner fileReader = new Scanner(new File(movies));
           Scanner lineReader;

           while(fileReader.hasNext()){
               info = fileReader.nextLine();

               lineReader = new Scanner(info);
               lineReader.useDelimiter(":");

               String title = lineReader.next();
               String director = lineReader.next();
               String genre = lineReader.next();
               int year = lineReader.nextInt();
            }

        }catch(FileNotFoundException error){
            System.out.println("File not found.");

        }catch(IOException error){
            System.out.println("Oops! Something went wrong.");
        }
    }



 public int countGenres(){
    String g;
    ArrayList <String> gList = new ArrayList<String>();
    for(Student s : movies){
      String g = s.getGenre();
      if(gList.contains(g) == false){
        gList.add(g);
      }
      return gList.size();
    }
    }
    public int countDirectors(){
     ArrayList <String> dList = new ArrayList<String>();
     for(Student s : movies){
        String d = s.getDirector();
        if(dList.contains(d) == false){
            dList.add(d);
        }
        return dList.size();
     }

     }

    public String listGenres(){
        ArrayList <String> genreList = new ArrayList<String>();

    }

}**

5 个答案:

答案 0 :(得分:2)

对于String方法中的循环,foreach变量的类型(searchTitle)必须与参数化类型的类型参数(MovieEntry)兼容( ArrayList<MovieEntry>)。事实显然并非如此。以下foreach循环可以工作:

for (MovieEntry title : Database) {
    System.out.println(title);
}

请考虑遵循为字段使用小写名称的惯例,即使用ArrayList<MovieEntry>而不是database来呼叫您的Database

答案 1 :(得分:1)

好吧

for (Type obj : Collection) {...}
仅当集合包含Type类型的元素时,

才有效。实际上这在你的情况下意味着:

for (MovieDatabase database : Database) {...}

将起作用,因为Database是ArrayList,其类型为MovieDatabase:

ArrayList<MovieDatabase> Database= new ArrayList<MovieDatabase>();

如果你想在数据库中循环标题,你必须提供一个方法来回放标题的字符串列表,例如

public ArrayList<String> getTitles() {...}
....
for (String title : Database.getTitles()) {...}

答案 2 :(得分:0)

您的数据库属性(应为数据库,小写),类型为:

ArrayList<MovieDatabase>

并且您正尝试使用String作为元素类型来迭代它。

你的foreach应该是:

for(MovieDatabase element: Database){
....
}

答案 3 :(得分:0)

你的for-each循环看起来应该是这样的(这只是一个例子,所以你必须为它调整它)

ArrayList<MovieEntry> list = new ArrayList<MovieEntry>();
...
for(MovieEntry m : list) {
   ...
}

ArrayList是一个有效的循环,但在您的情况下,当您的列表未保留Strings时,您正试图获取Strings,那么您正在调用Strings上的方法{1}} String不支持。您必须为每个列表使用正确的类型。

答案 4 :(得分:0)

更改

private ArrayList<MovieEntry> Database = new ArrayList<MovieEntry>();
public MovieDatabase(){
   ArrayList<MovieDatabase> Database = new ArrayList<MovieDatabase>(0);
}

private ArrayList<MovieEntry> Database;;
public MovieDatabase(){
   Database = new ArrayList<MovieDatabase>(0);
}