2014-11-09 76 views
1

我想調用showArrayList方法,但它不斷給我ContactList無法轉換爲java.util.ArrayList<Contact>。我究竟做錯了什麼?到目前爲止,我有這樣的:試圖調用showArrayList方法

public class ControlPanel extends JPanel implements ActionListener 
{ 
    // Fields (instance variables/attributes) 
    private JLabel  phoneNumberLabel; // a reference to the phoneNumberLabel that is displayed in the PhoneNumberDisplayPanel. 

    private Contact  nameAndNumber; // Holds the name and number for a phone Contact. 
    // Contact lists; 
    private ContactList contacts;   // Encapsulates a list of saved contacts, ie.e phone Contacts. 
    private ContactList callsMade;   // Encapsulates a list of calls made, i.e. phone Contacts. 

private void findNumber() 
    { 
     if(nameLabel.getText().equals(contacts.searchByName(nameLabel.getText()))) 
      showArrayList(contacts,nameLabel.getText()); 
     else 
      showArrayList(contacts, ""); 
    } 

showArrayList方法是:

public void showArrayList(ArrayList<Contact> list, String title) 
    { 
     int x = 410; 
     int y = 445; 
     int width = 350; 
     int height = 200; 
     boolean disableCloseButton = false; 
     Window newWindow = new Window(title, x, y, width, height, Color.RED, disableCloseButton); 
     { 
      newWindow.println("No contact information saved"); 
     } 
     else 
     { 
      for (Contact c: list) 
      { 
       newWindow.println(c.toString()); 
      } 
     } 

現在我做到這一點,它編譯:

private void findNumber() 
    { 
     ArrayList<Contact> list = new ArrayList<Contact>(); 
     if(nameLabel.getText().equals(contacts.searchByName(nameLabel.getText()))) 
      showArrayList(list,nameLabel.getText()); 
     else 
      showArrayList(list, ""); 
    } 

我ContactList類:

public class ContactList 
{ 
    private ArrayList<Contact> list; 

    /** 
    * Constructor for a ContactList, to initialize the list from file. 
    * 
    * @param fileName a reference to an existing file 
    * @param window  a reference to an existing window to display the list contacts 
    */ 
    public ContactList(String fileName, Window window) 
    { 
     list = new ArrayList<Contact>(); 

     File file = new File(fileName); 

     // Remark. A file needs to have been written, before it can be read. 
     if (file.exists()) 
     { 
      try 
      { 
       Scanner fileReader = new Scanner(file); 
       readFile(fileReader); 
       window.print(this.toString()); 
      } 
      catch(FileNotFoundException error) // could not find file 
      { 
       System.out.println("File not found "); 
      } 
     } 
    } 

    /******************************************************************** 
    * Searches the list for a particular contact, 
    * comparing this name and number (in lower case) 
    * with the contact name and number (in lower case). 
    * 
    * @param contact 
    * @return returns true if the contact is found, otherwise false 
    */ 
    public boolean found(Contact contact) 
    { 
     for(Contact c: list){ 
      if(c == contact){ 
       return true; 
      } 
     } 
     return false; 

    } 

    /****************************************************************** 
    * Adds one contact to the list 
    * 
    * @param contact 
    */ 
    public void add(Contact contact) 
    { 
     list.add(contact); 


    } 

    /****************************************************************** 
    * searches the list for each name that contains the specified substring 
    * 
    * @param substring 
    * @return returns the result as an ArrayList 
    */ 
    public ArrayList<Contact> searchByName(String substring) 
    { 
     ArrayList<Contact> l = new ArrayList<Contact>(); 
     for(Contact c: list){ 
      if(c.getName() == substring){ 
       l.add(c); 
      } 
     } 
     return l; 
    } 
+0

告訴我們在哪兒聲明'contacts' – 2014-11-09 05:01:22

+0

編輯它變成你的問題,而不是作爲一個評論(讀取硬盤作爲註釋)。 'contacts'是一個'ContactList',而不是'ArrayList ',這就是爲什麼你的方法不接受它 – 2014-11-09 05:08:07

+0

我已經嘗試了一切。我試過我的實際ArrayList(從我的ContactList類)。也許我應該創建一個新的ArrayList? – phoenix47 2014-11-09 05:11:40

回答

0

ContactList不延伸ArrayList<Contact>,因此您無法將其作爲參數傳遞給期望ArrayList<Contact>的方法。也許你可以在ContactList中有一個getter來公開list,這樣你就可以將它傳遞給你的showArrayList方法,或者你可以實現一個showArrayList方法來加入ContactList類型的argument

0
private void findNumber() 
    { 
     if(nameLabel.getText().equals(contacts.searchByName(nameLabel.getText()))) 
      showArrayList(contacts.searchByName(""),nameLabel.getText()); 
     else 
      showArrayList(contacts.searchByName(""), ""); 
    }