gpt4 book ai didi

Java array.add() 停止程序

转载 作者:行者123 更新时间:2023-12-01 18:34:06 25 4
gpt4 key购买 nike

我设法在数组中动态添加一个元素,但不幸的是程序停止了。我怎样才能继续呢?谢谢!

...

List<String> messages = Arrays.asList("Mary", "Nadia!", "Drake");

System.out.println("Enter Costumer's Name:");
cust_name =in.next();
if(cust_name .equals("Mary"))
System.out.println("Already a member!");
else if (cust_name .equals("Nadia"))
System.out.println("Already a member!");
else if(cust_name .equals("Drake"))
System.out.println("Already a member!");
else
messages.add(in.next());
System.out.println("Not a member! " + cust_name + " just added.");

...

最佳答案

Arrays.asList()返回固定大小的List,我能想到的最简单的解决方法是 -

List<String> messages = new ArrayList<String>(
Arrays.asList("Mary", "Nadia!", "Drake")
); // Creates a new (mutable) list.

关于Java array.add() 停止程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795501/

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