【问题标题】:"Actual or formal argument lists differs in length"“实际或形式参数列表的长度不同”
【发布时间】:2013-07-18 22:26:34
【问题描述】:

当我尝试在 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;
    }
}

【问题讨论】:

  • 你需要为Friends(创建一个构造函数。

标签: java arrays arguments


【解决方案1】:

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

    public Friends( String firstName, String age) { ... }

【讨论】:

  • 他确实有一些属性。它们只是位于 getter/setter 上方而不是顶部。
【解决方案2】:

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

Friends f = new Friends(friendsName, friendsAge);

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

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

【讨论】:

  • 太棒了。谢谢您的帮助!你知道为什么“friendsList.remove(input.next());”不开心吗?
  • 您无法从包含 Friend 对象的列表中删除 String。您必须遍历 Friend 列表并找到名称与输入的名称相同的 Friend
  • 请注意,我认为“空构造函数 + 修饰符”是一种反模式。我不认为能够构造无法使用的对象,或者会导致错误或其他意外,直到它们在后续(可能被遗忘的)步骤中初始化。
  • 我也不是粉丝,但它们在某些类型的程序中有它们的用途(想想 java bean 和 dtos)
  • 我遇到了类似的问题。但是构造函数在我的项目所依赖的另一个项目中。 Eclipse 没有抱怨,但我在 mvn install 上获得了编译时间。我想知道这怎么可能。尽管我使用接受的答案中提到的设置器解决了这个问题,但我只是想知道出了什么问题。
【解决方案3】:

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

    @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);

【讨论】:

    【解决方案4】:

    如果你和我一样使用匕首,可能会出现这种情况。这意味着,首先您有一个参数,然后将第二个参数添加到 dagger 构造函数。因此,当 dagger 自动生成代码时,它会给出错误 - “依赖注入实际参数列表和形式参数列表长度不同”

    你应该

    1. 无效和缓存
    2. 重建项目

    它有助于 dagger 识别您新添加的参数,然后您的项目再次运行))

    【讨论】:

      猜你喜欢
      • 2014-04-06
      • 2019-08-10
      • 2023-03-25
      • 2016-05-31
      • 2018-05-08
      • 2013-10-04
      • 1970-01-01
      相关资源
      最近更新 更多