gpt4 book ai didi

java - 从java中的另一个类中删除arraylist中的指定对象

转载 作者:行者123 更新时间:2023-11-29 04:18:56 24 4
gpt4 key购买 nike

我想根据用户输入的内容从另一个类中删除数组列表中的指定数据所以当用户输入 Rice 时,其中包含 'Rice' 的数据将被删除这是到目前为止的代码

数据存储在名为 RegularMenu 的子类中的 ArrayList rm 中。

ArrayList<RegularMenu> rm = new ArrayList<RegularMenu>();

Quiz1(){
int input;
do{
System.out.println("1. Add Regular Menu");
System.out.println("2. Add Special Menu");
System.out.println("3. Show All Menu");
System.out.println("4. Delete Regular Menu");
System.out.println("5. Delete Special Menu");
System.out.println("6. Exit" + "\n");

System.out.print("Choice [1-6] ");
Scanner s = new Scanner(System.in);
input = s.nextInt();

if (input == 1 ){

String code, name;
int price;

System.out.println("Add Regular Menu");
System.out.print("Input Menu Code [R...]: ");
s.nextLine();
code = s.nextLine();

System.out.print("Input Menu Name [5-20]: ");
name = s.nextLine();

System.out.print("Input Menu Price [10000-130000]: ");
price = s.nextInt();

rm.add(new RegularMenu(code, name, price));
}

else if (input == 2){

}

else if (input == 3){

}

else if (input == 4){


System.out.println("Input menu code you want to delete");
String a = s.nextLine();

for(int i=0;i<rm.size();i++){
if(a == rm.get(i).getCode()){
String code = rm.get(i).getCode();
a = code;
rm.remove(a);
}

}

}

else if (input == 5){

}
}while(input != 6);
}

我可以添加数据,但是当尝试删除它时,发生了错误。如果我不够清楚,请告诉我。

最佳答案

问题是您正在使用 List.remove(Object)错误而不是 List.remove(int) .

List.remove(对象)

boolean remove(Object o)

Removes the first occurrence of the specified element from this list, if it is present (optional operation). [...] More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) [...]

尝试删除带有 String 实例的 RegularMenu 是行不通的,因为 String 实例无法将自身与 进行比较>RegularMenu,所以它永远不会等价。 (它将使用 String.equals(RegularMenu) 方法来查找要删除的实例的位置。

如果你想使用List.remove(Object),传递实例本身:

rm.remove(rm.get(i));

注意:

  1. 这只会删除第一个实例,因此如果您有两个“等效”实例,则只会删除第一个实例。
  2. List.remove(Object) 将搜索传递的实例使用 Object.equals 按索引删除的索引。但在您的情况下,您已经使用 if(a == rm.get(i).getCode()) 完成了工作(不正确,请参阅“字符串比较”),

列表.remove(int)

E remove(int index)

Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices) [...]

因为您知道索引,所以可以使用 rm.remove(i) (List.remove(int)) 删除当前索引处的值。

以这种方式小心索引,列表的右侧部分在左侧移动。参见 Lajos Arpad's answer了解更多信息

迭代器

ArrayList 中删除项目的另一种解决方案是使用迭代器。您获得 Iterator 并迭代其中的每个项目。然后,如果有一个匹配,您调用 Iterator.remove 以正确删除该项目。如果您只想删除一个项目,您甚至可以停止循环

示例数据:

List<String> datas = new ArrayList<>();
datas.add("foo");
datas.add("bar");
datas.add("bar");
datas.add("foo");
datas.add("bar");
datas.add("bar");

代码:

Iterator<String> it = datas.iterator();
String s;
while(it.hasNext()){
s = it.next();
if("foo".equals(s)){
it.remove();
}
}

System.out.println(datas);

[bar, bar, bar, bar]

我使用 ArrayList 是因为一些 Collection 没有为 Iterator 实现方法 remove 异常

谓词 - removeIf

从 Java 8 开始,Collection.removeIf存在并允许你做一个更快的解决方案(使用相同的样本数据):

final String a = "foo";
datas.removeIf(s -> a.equals(s));

System.out.println(datas);

[bar, bar, bar, bar]

它将迭代并检查其中的每个 RegularMenu 实例,如果传递的 Predicatetrue,如果是,则该项目将被删除。


字符串比较

另外,请注意比较 "foo".equals(s) 而不是 "foo"== s
更多信息请参阅 How do I compare strings in Java?

关于java - 从java中的另一个类中删除arraylist中的指定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50465608/

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