27

当我尝试将某些内容放在它的 () 括号中时Friends f = new Friends(friendsName, friendsAge);,会出现错误:

Friends 类中的构造函数 Friends 不能应用于给定类型。必需:无参数。找到:字符串,整数。原因:实际或形式参数列表的长度不同。

但是当我取出参数时,我的朋友列表只显示“null 0”。即使我有这些值也没有设置String friendsName = input.next();吗?

此外,当我尝试删除朋友时,它什么也没做。在源代码中它确实提出了一个警告,

对 util.java.Collection.remove 的可疑调用:给定对象不能包含给定的字符串实例(预期的朋友)。

我对这一切意味着什么感到困惑?

import java.util.ArrayList;
import java.util.Scanner;

public class Friends
{
    public static void main( String[] args )
    {
        int menu;       
        int choice;
        choice = 0;      

        Scanner input = new Scanner(System.in);
        ArrayList< Friends > friendsList = new ArrayList<  >();       

        System.out.println(" 1. Add a Friend ");
        System.out.println(" 2. Remove a Friend ");
        System.out.println(" 3. Display All Friends ");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

        while(menu != 4)
        {    

            switch(menu)
            {                     

            case 1:

                while(choice != 2)
                {
                    System.out.println("Enter Friend's Name: ");
                    String friendsName = input.next();
                    System.out.println("Enter Friend's Age: ");
                    int friendsAge = input.nextInt();                               
                    Friends f = new Friends(friendsName, friendsAge);
                    friendsList.add(f);
                    System.out.println("Enter another? 1: Yes, 2: No");
                    choice = input.nextInt();
                } break;

            case 2:

                System.out.println("Enter Friend's Name to Remove: ");
                friendsList.remove(input.next());                   
                break;   

            case 3:

                for(int i = 0; i < friendsList.size(); i++)
                {
                    System.out.println(friendsList.get(i).name + " " + friendsList.get(i).age);                        
                } break;                
        }

        System.out.println(" 1. Add a Friend ");
        System.out.println(" 2. Remove a Friend ");
        System.out.println(" 3. Display All Friends ");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

    }

    System.out.println("Thank you and goodbye!");

}

    public String name;
    public int age;    

    public void setName( String friendsName )
    {
        name = friendsName;
    } 
    public void setAge( int friendsAge )
    {
        age = friendsAge;
    }
    public String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
    }
}
4

4 回答 4

22

您尝试Friends像这样实例化类的对象:

Friends f = new Friends(friendsName, friendsAge);

该类没有带参数的构造函数。您应该添加构造函数,或者使用确实存在的构造函数创建对象,然后使用 set-methods。例如,代替上面的:

Friends f = new Friends();
f.setName(friendsName);
f.setAge(friendsAge);
于 2013-07-19T15:08:58.223 回答
2

默认构造函数没有参数。您需要指定一个构造函数:

    public Friends( String firstName, String age) { ... }
于 2013-07-19T15:08:25.073 回答
1

假设你已经这样定义了你的类:

    @Data
    @AllArgsConstructor(staticName = "of")
    private class Pair<P,Q> {

        public P first;
        public Q second;
    }

因此,当您需要创建一个新实例时,它需要接受参数,并且您将像注释中定义的那样提供它。

Pair<Integer, String> pair = Pair.of(menuItemId, category);

如果你这样定义它,你会得到要求的错误。

Pair<Integer, String> pair = new Pair(menuItemId, category);
于 2020-05-10T03:26:36.603 回答
0

If you are working with daggger like me, such condition may occur. This meant that, first you have one parameter, and then you add second parameter to dagger constructor. So when dagger auto generate code, it gives error - " dependency injection actual and formal argument lists differ in length "

You should

  1. invalidate and caches
  2. Rebuid-project

it helps dagger to identify your new added paramter, then your project works again ))

于 2021-12-22T06:29:13.153 回答