gpt4 book ai didi

java - 为什么我的 Arraylist 类不工作?我究竟做错了什么?

转载 作者:行者123 更新时间:2023-11-30 02:45:55 25 4
gpt4 key购买 nike

我的任务是创建一个带有选项菜单的来电跟踪系统。每个电话都应该属于 ArrayList显示用户的姓名和电话号码。我的第一个困难是将名称(字符串)和数字( double )存储在 ArrayList 中。一起。经过一番尝试,这就是我想出的——但是我的第三堂课removeadd方法不起作用?我做错了什么?我在网上查看了示例,但不明白为什么 removeadd方法不起作用。

我的第三堂课:我的问题在哪里

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

最佳答案

您的 Person 类没有任何名为 addremove 的方法,因此您无法调用 Person.add Person.remove。相反,您应该向列表本身添加或删除项目。

由于您是从命令提示符读取调用者数据,因此您必须弄清楚用户输入的文本所指的是哪个人。假设他们输入类似 "John,555- 5555”,您可以基于此为 John 构造一个新的 Person 对象。使用String#split ,根据逗号的位置分割文本,然后创建一个新的 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);
}
}

关于java - 为什么我的 Arraylist 类不工作?我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40197885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com