[英]Delete specified object in arraylist from another class in java
我想根據用戶輸入的內容從另一個類中刪除arraylist中的指定數據,所以當用戶輸入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)
。
從該列表中刪除第一次出現的指定元素(如果存在)(可選操作)。 [...]更正式地,刪除具有最低索引i的元素,使得
(o==null ? get(i)==null : o.equals(get(i)))
[...]
嘗試使用String
實例刪除RegularMenu
將無法工作,因為String
實例無法將自身與RegularMenu
進行比較,因此它永遠不會等效。 (它將使用String.equals(RegularMenu)
方法來查找要刪除的實例的位置。
如果要使用List.remove(Object)
,請傳遞實例本身:
rm.remove(rm.get(i));
注意:
List.remove(Object)
將搜索實例傳遞的索引,使用Object.equals
按索引刪除。 但在你的情況下,你已經使用if(a == rm.get(i).getCode())
(錯誤地看到“ 字符串比較 ”), 刪除此列表中指定位置的元素(可選操作)。 將任何后續元素向左移位(從索引中減去一個)[...]
由於您知道索引,因此可以使用rm.remove(i)
( rm.remove(i)
List.remove(int)
)來刪除當前索引處的值。
小心那樣的索引,列表的右邊部分左移。 有關更多信息,請參閱Lajos Arpad的答案
從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);
[酒吧,酒吧,酒吧,酒吧]
我精確地使用ArrayList
因為一些Collection
沒有為Iterator
提供Exception
的方法remove
。
從Java 8開始, Collection.removeIf
存在並允許您執行更快的解決方案(使用相同的示例數據):
final String a = "foo";
datas.removeIf(s -> a.equals(s));
System.out.println(datas);
[酒吧,酒吧,酒吧,酒吧]
如果傳遞的Predicate
為true
,它將迭代並檢查其中的每個RegularMenu
實例,如果是,則將刪除該項。
另外,請注意比較"foo".equals(s)
而不是"foo" == s
。
有關如何比較Java中的字符串的更多信息?
您可以通過索引從List中刪除元素
for(int i=rm.size()-1; i>=0; i--) {
// you are deleting elements from the list while iterating,
// thus it is better to iterate backwards (rm.size()..0):
if(a.trim().equals(rm.get(i).getCode().trim())) {
rm.remove(i);
}
}
注意:當您刪除索引i
下的元素時,右側的所有元素(i+1, ...)
將向左移動一個位置。
因此,當從左向右迭代並刪除元素時,您將陷入索引。
另一方面,當您從右向左迭代並刪除位置i
處的某些內容時,從右到右的所有元素仍然會向左移動一個位置,但這對您來說無關緊要,因為您不會迭代它們。
aaaaBcccc -> aaaacccc
^ ^ ^ ^
0.. i 0.. i
您可以使用循環迭代數組列表並刪除所有匹配項:
for (int i = 0; i < yourArrayList.size(); i ++) {
if(a.equals(rm.get(i).getCode())){
yourArrayList.remove(i--);
}
}
你的錯誤是,你想remove
a
從ArrayList
使用remove
,但remove
預期的int
,代表索引中刪除。 在我的代碼的通知,我遞減i
刪除,以便能夠后remove
隨之而來的比賽。
for(int i=0;i<rm.size();i++) {
if(a.trim().equals(rm.get(i).getCode())) {
rm.remove(i);
break;
}
}
在列表中,您可以使用索引刪除元素。
查找以下代碼:
for(RegularMenu regularMenu:rm){
if(a.equalsIgnoreCase(regularMenu.getCode())){
rm.remove(regularMenu);
break;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.