java – “实际或正式参数列表的长度不同”
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – “实际或正式参数列表的长度不同”,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2487字,纯文字阅读大概需要4分钟。
内容图文
![java – “实际或正式参数列表的长度不同”](/upload/InfoBanner/zyjiaocheng/711/434125de4afa446f914fc2e69135f888.jpg)
当我尝试在朋友f(新朋友)(friendsName,friendsAge)的()括号中放置一些东西时;它出现了错误:
Constructor Friends in class Friends cannot by applied to given types.
Required: no arguments. Found: String, int. Reason: actual or formal
argument lists differ in length.
但是当我取出参数时,我的朋友列表只显示“null 0”.即使我有String friendsName = input.next();?是否设置了值?
此外,当我尝试删除朋友时,它不会做任何事情.在源代码中,它确实会发出警告,
Suspicious call to util.java.Collection.remove: Given object cannot
contain given instances of String (expected Friends).
我对这一切意味着什么感到困惑?
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类的对象,如下所示:
Friends f = new Friends(friendsName, friendsAge);
该类没有带参数的构造函数.您应该添加构造函数,或者使用确实存在的构造函数创建对象,然后使用set-methods.例如,而不是上述:
Friends f = new Friends();
f.setName(friendsName);
f.setAge(friendsAge);
内容总结
以上是互联网集市为您收集整理的java – “实际或正式参数列表的长度不同”全部内容,希望文章能够帮你解决java – “实际或正式参数列表的长度不同”所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。