Java &引用;实际或正式参数列表的长度不同“;

Java &引用;实际或正式参数列表的长度不同“;,java,arrays,arguments,Java,Arrays,Arguments,当我试图把一些东西放在Friends的()括号中时,f=newfriends(friendsName,friendsAge)出现错误: 类Friends中的构造函数Friends不能应用于给定类型。 必需:无参数。找到:字符串,整数。原因:实际或正式 参数列表长度不同 但当我取出参数时,我的朋友列表只显示“null 0”。即使我有String friendsName=input.next(),也没有设置这些值 而且,当我试图删除一个朋友时,它不会起任何作用。在源代码中,它确实会发出警告 对uti

当我试图把一些东西放在
Friends的()括号中时,f=newfriends(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;
    }
}
import java.util.ArrayList;
导入java.util.Scanner;
公课朋友
{
公共静态void main(字符串[]args)
{
int菜单;
智力选择;
选择=0;
扫描仪输入=新扫描仪(System.in);
ArrayListfriendsList=新建ArrayList<>();
System.out.println(“1.添加朋友”);
System.out.println(“2.删除好友”);
System.out.println(“3.显示所有好友”);
System.out.println(“4.Exit”);
menu=input.nextInt();
while(菜单!=4)
{    
开关(菜单)
{                     
案例1:
while(选项!=2)
{
System.out.println(“输入朋友的姓名:”);
String friendsName=input.next();
System.out.println(“输入朋友的年龄:”);
int-friendsAge=input.nextInt();
Friends f=新朋友(friendsName,friendsAge);
加上(f);
System.out.println(“输入另一个?1:是,2:否”);
choice=input.nextInt();
}中断;
案例2:
System.out.println(“输入要删除的朋友的姓名:”);
friendsList.remove(input.next());
打破
案例3:
对于(int i=0;i
默认构造函数没有参数。您需要指定一个构造函数:

publicfriends(stringfirstname,stringage){…}

您尝试实例化
Friends
类的对象,如下所示:

Friends f = new Friends(friendsName, friendsAge);
    @Data
    @AllArgsConstructor(staticName = "of")
    private class Pair<P,Q> {

        public P first;
        public Q second;
    }
该类没有接受参数的构造函数。您应该添加构造函数,或者使用确实存在的构造函数创建对象,然后使用set方法。例如,与上述内容不同:

Friends f = new Friends();
f.setName(friendsName);
f.setAge(friendsAge);

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

Friends f = new Friends(friendsName, friendsAge);
    @Data
    @AllArgsConstructor(staticName = "of")
    private class Pair<P,Q> {

        public P first;
        public Q second;
    }
@数据
@AllArgsConstructor(staticName=“of”)
私有类对{
公共政策优先;
公众Q秒;
}
因此,当您需要创建一个新实例时,它将需要获取参数,并且您将按照注释中的定义提供它

Pair<Integer, String> pair = Pair.of(menuItemId, category);
Pair-Pair=Pair.of(menuItemId,类别);
如果您这样定义它,您将得到请求的错误

Pair<Integer, String> pair = new Pair(menuItemId, category);
Pair Pair=新对(menuItemId,类别);

你需要为
朋友(
)创建一个构造函数。他实际上有一些属性。它们只是位于getter/setter上方,而不是顶部。太棒了。谢谢你的帮助!你知道为什么会出现“friendsList.remove(input.next());”不高兴吗?您不能从包含<代码>朋友< /Cord>对象的列表中删除<代码>字符串<代码>。您必须重复“<代码>朋友< /代码>的列表,并找到与输入的名称相同的<代码>朋友< /代码>。请注意,我认为“空构造函数+修饰符”。一种反模式。我认为在后续(可能被遗忘)步骤中初始化之前,能够构造无法使用或会导致错误或其他意外的对象不是一个好主意。我也不是一个爱好者,但它们在某些类型的程序中有其用途(想想java beans和DTO)我有一个类似的问题。但构造函数存在于我的项目所依赖的另一个项目中。Eclipse没有抱怨,但我在mvn安装上获得了编译时间。我想知道这是怎么可能的。虽然我在接受的答案中提到了使用setter解决了这个问题,但我只是好奇到底出了什么问题。