Java 为什么不是';我的Arraylist班不工作吗?我做错了什么?
我的任务是创建一个带有选项菜单的来电跟踪系统。每个电话都应该属于一个显示用户名和电话号码的Java 为什么不是';我的Arraylist班不工作吗?我做错了什么?,java,arraylist,Java,Arraylist,我的任务是创建一个带有选项菜单的来电跟踪系统。每个电话都应该属于一个显示用户名和电话号码的ArrayList。我的第一个难题是将名称(字符串)和数字(双精度)存储在ArrayList中。在玩过之后,这就是我想到的-但是我的第三个类remove和add方法不起作用?我做错了什么?我在网上查看了一些示例,不明白为什么remove和add方法不起作用 我的第三节课:我的问题在哪里 public class Incoming { Scanner input = new Scanner(Syst
ArrayList
。我的第一个难题是将名称(字符串)和数字(双精度)存储在ArrayList
中。在玩过之后,这就是我想到的-但是我的第三个类remove
和add
方法不起作用?我做错了什么?我在网上查看了一些示例,不明白为什么remove
和add
方法不起作用
我的第三节课:我的问题在哪里
public class Incoming {
Scanner input = new Scanner(System.in);
ArrayList<Person> arr = new ArrayList<Person>();
Person p1 = new Person("Alex", "01010101");
Person p2 = new Person("Emily", "0123812"); // I will have 10 people
void AddCall() {
System.out.println("Who would you like to add to the call? Enter p+number");
String add = input.nextLine();
Person.add(input);
}
void RemoveCall() {
System.out.println("Which call would you like to answer? Enter p+ caller position"); //NOTE following will be removed from queue
String remove = input.nextLine();
Person.remove(input);
}
void ViewCallerList() {
System.out.println("The queue has the following callers: " + Person);
}
}
公共类传入{
扫描仪输入=新扫描仪(System.in);
ArrayList arr=新的ArrayList();
人员p1=新人员(“Alex”,“01010101”);
Person p2=新人(“Emily”,“0123812”);//我将有10个人
void AddCall(){
System.out.println(“您想将谁添加到呼叫中?输入p+号码”);
字符串add=input.nextLine();
添加(输入);
}
void RemoveCall(){
System.out.println(“您想接听哪个电话?输入p+呼叫者位置”);//注意以下内容将从队列中删除
String remove=input.nextLine();
人。删除(输入);
}
void ViewCallerList(){
System.out.println(“队列有以下调用方:“+Person”);
}
}
您的Person
类没有任何名为add
或remove
的方法,因此您永远不能调用Person.add
或Person.remove
。相反,您应该在列表本身中添加和删除项目
由于您正在从命令提示符读取呼叫者数据,因此必须找出用户键入的文本所指的是哪个人。假设他们键入类似于“John,555-5555”
,您可以基于此为John构造一个新的Person
对象。使用,根据逗号的位置拆分文本,然后创建一个新的Person
实例以添加到呼叫者列表中:
public class Incoming {
Scanner input = new Scanner(System.in);
List<Person> callers = new ArrayList<Person>();
Person p1 = new Person("Alex", "01010101");
Person p2 = new Person("Emily", "0123812"); // I will have 10 people
private static Person readPerson(Scanner sc) {
String callerText = sc.nextLine();
String[] callerData = callerText.split(",");
return new Person(callerData[0], callerData[1]);
}
void addCall() {
System.out.println("Who would you like to add to the call? Enter p+number");
callers.add(readPerson(input));
}
void removeCall() {
// Complete this based on the add method above
}
// This should output the list (callers), not a single person
void viewCallerList() {
System.out.println("The queue has the following callers: " + callers);
}
}
公共类传入{
扫描仪输入=新扫描仪(System.in);
列表调用者=新的ArrayList();
人员p1=新人员(“Alex”,“01010101”);
Person p2=新人(“Emily”,“0123812”);//我将有10个人
私人静态个人阅读器(扫描仪sc){
字符串callerText=sc.nextLine();
字符串[]callerData=callerText.split(“,”);
返回新的人员(callerData[0],callerData[1]);
}
void addCall(){
System.out.println(“您想将谁添加到呼叫中?输入p+号码”);
add(readPerson(input));
}
void removeCall(){
//根据上面的add方法完成此操作
}
//这应该输出列表(呼叫者),而不是一个人
void viewCallerList(){
System.out.println(“队列有以下调用方:“+调用方”);
}
}
您的Person
类没有任何名为add
或remove
的方法,因此您永远不能调用Person.add
或Person.remove
。相反,您应该在列表本身中添加和删除项目
由于您正在从命令提示符读取呼叫者数据,因此必须找出用户键入的文本所指的是哪个人。假设他们键入类似于“John,555-5555”
,您可以基于此为John构造一个新的Person
对象。使用,根据逗号的位置拆分文本,然后创建一个新的Person
实例以添加到呼叫者列表中:
public class Incoming {
Scanner input = new Scanner(System.in);
List<Person> callers = new ArrayList<Person>();
Person p1 = new Person("Alex", "01010101");
Person p2 = new Person("Emily", "0123812"); // I will have 10 people
private static Person readPerson(Scanner sc) {
String callerText = sc.nextLine();
String[] callerData = callerText.split(",");
return new Person(callerData[0], callerData[1]);
}
void addCall() {
System.out.println("Who would you like to add to the call? Enter p+number");
callers.add(readPerson(input));
}
void removeCall() {
// Complete this based on the add method above
}
// This should output the list (callers), not a single person
void viewCallerList() {
System.out.println("The queue has the following callers: " + callers);
}
}
公共类传入{
扫描仪输入=新扫描仪(System.in);
列表调用者=新的ArrayList();
人员p1=新人员(“Alex”,“01010101”);
Person p2=新人(“Emily”,“0123812”);//我将有10个人
私人静态个人阅读器(扫描仪sc){
字符串callerText=sc.nextLine();
字符串[]callerData=callerText.split(“,”);
返回新的人员(callerData[0],callerData[1]);
}
void addCall(){
System.out.println(“您想将谁添加到呼叫中?输入p+号码”);
add(readPerson(input));
}
void removeCall(){
//根据上面的add方法完成此操作
}
//这应该输出列表(呼叫者),而不是一个人
void viewCallerList(){
System.out.println(“队列有以下调用方:“+调用方”);
}
}
我认为您混淆了数组列表的add和remove方法与Person类的add和remove方法,这就是为什么它看起来“不起作用”。您需要执行arr.remove()
,除非在使用内置的.add()
和.remove
列表方法时不能使用字符串输入。您应该在AddCall()
和RemoveCall()中创建一个新的Person
对象
方法,然后将其添加/删除到阵列列表中properly@Droidman你好,谢谢你的回复。嗯,我相信Arraylist这个人也应该在我的主课上。我可以这样称呼它吗?@KiroYakuza哦,我明白了!谢谢,我现在就试试:)@Tom代码已经编辑好了。但是无论如何,Person.add(输入)代码>和人员。删除(输入)代码>实际上是静态方法调用。我认为您混淆了数组列表的add和remove方法与Person类的add和remove方法,这就是为什么它看起来“不起作用”。您需要执行arr.remove()
,除非在使用内置的.add()
和.remove
列表方法时不能使用字符串输入。您应该在AddCall()
和RemoveCall()中创建一个新的Person
对象
方法,然后将其添加/删除到阵列列表中