Java 正在尝试调用showArrayList方法
我正在尝试调用showArrayList方法,但它一直告诉我ContactList无法转换为java.util.ArrayList。我做错了什么?到目前为止,我有:Java 正在尝试调用showArrayList方法,java,arraylist,Java,Arraylist,我正在尝试调用showArrayList方法,但它一直告诉我ContactList无法转换为java.util.ArrayList。我做错了什么?到目前为止,我有: public class ControlPanel extends JPanel implements ActionListener { // Fields (instance variables/attributes) private JLabel phoneNumberLabel; // a refe
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, "");
}
我的联系人列表类:
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;
}
ContactList不扩展ArrayList,因此不能将其作为参数传递给需要ArrayList的方法。也许您可以在ContactList中使用一个getter来公开该列表,以便将其传递给showArrayList方法,或者您可以实现一个showArrayList方法来接受ContactList类型的参数。向我们展示您在何处声明ContactsIt,而不是将其作为难以理解的注释。contacts是一个ContactList,而不是ArrayList,这就是为什么您的方法不接受itI的原因我已经尝试了所有方法。我已经从ContactList类中尝试了实际的ArrayList。也许我应该创建一个新的ArrayList?显示您尝试使用ArrayList的位置,该列表不是ContactList对象中的列表;这是一个全新的列表。你不应该因为这样做而出错,尽管你不会访问你想要的值,因为它不是正确的列表。您的联系人列表应该有一个ArrayList,如果它是私有的,则有一个访问它的方法
private void findNumber( )
{
if(nameLabel.getText().equals(contacts.searchByName(nameLabel.getText())))
showArrayList(contacts.searchByName(""),nameLabel.getText());
else
showArrayList(contacts.searchByName(""), "");
}