【发布时间】:2019-08-15 01:30:18
【问题描述】:
如何删除我之前在 ArrayList 中添加的元素 我这样创建它:
public static ArrayList<Product> P = new ArraList<Product>();
我使用的方法:
public void removeProduct(Product p) {
P.remove(p); // this way, did not solve the problem
}
//我做了(添加方法),它工作正常,一切都很好,我希望有人能帮助得到答案,谢谢:)
public void deleteProduct(String ID) {
System.out.println("Enter product id to delete: ");
ID = input.next();
for(Product m : s.P) {
if(ID.equals(m.getID())) {
s.P.remove(ID);
}
else {
System.out.println("ID is not exist");
}
}
}
//和
public void removeProductToCart(Product p) {
viewShoppingCart();
System.out.println("Enter product id to remove it: ");
String ID = input.next();
for(Product r : s.P) {
if(ID.equals(r.getID())) {
s.P.remove(p);
}
else {
System.out.println("ID is not exist");
}
}
}
【问题讨论】:
-
具体问题是什么。 “我需要帮助”并没有告诉我们您所面临的问题。你在做什么,你期望发生什么,反而会发生什么?准确。
-
你写
I did the (added the method) and it works and everything is fine。如果是all fine,你需要什么? -
可能抛出异常。如果是这样,了解它会有所帮助(消息,在哪里,...)
标签: java