2012-03-29 47 views
0

我正在創建一個爲電影創建虛擬數據庫的項目。我有兩個類:MovieEntry(用於單獨的電影條目)和MovieDatabase(包含數據庫並允許添加的較大類等)。我遇到了一些錯誤,其中第一個是在searchTitle方法中它說數據庫是不兼容的類型。任何人都可以告訴我如何爲每個循環執行這些操作?我讀了這本書,我認爲ArrayList應該去那裏,但顯然不是。For-Each Loop不兼容類型

**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>(); 

    } 

}** 
+0

您可以添加「電影」的聲明。 – hmjd 2012-03-29 14:00:39

回答

0

你的數據庫屬性(應該是數據庫,小寫),類型爲:

ArrayList<MovieDatabase> 

,且您嘗試使用String作爲元素類型在它進行迭代。

你的foreach應該是:

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

for-each循環應該是這個樣子(這只是一個例子,所以你必須適應它爲你)

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

ArrayList是一個有效的事情來循環,但在你的情況下,你試圖得到Strings當你的列表不能保存Strings那麼你正在調用那些Strings方法String不支持。你必須爲每個列表使用正確的類型。

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); 
} 
2

類型在foreach可變的(String,在您的searchTitle方法中的循環的情況下)必須與類型參數兼容(MovieEntry)的參數化類型(ArrayList<MovieEntry>)。這顯然不是這樣。下面的foreach循環將工作:

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

請考慮使用小寫名字字段的約定如下,即打電話給你ArrayList<MovieEntry>使用一個名稱,如database而不是Database

0

for (Type obj : Collection) {...} 

工作只有在收集保存類型爲類型的元素。這實際上是在你的情況是指:

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

會工作,因爲數據庫是ArrayList的持有型MovieDatabase:

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

,如果你想循環遍歷數據庫冠軍,你必須提供還給方法例如標題的字符串列表,例如

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