英文:
Java How to delete object-type ArrayList with condition
问题
import java.util.*;
public class Main
{
public static class Item{
private String rackNumber, itemNumber;
private int i;
Item(String rackNumber, String itemNumber, int i){
this.rackNumber = rackNumber;
this.itemNumber = itemNumber;
this.i = i;
}
public String getRackNumber(){return rackNumber;}
public String getItemNumber(){return itemNumber;}
public int getI(){return i;}
@Override
public String toString(){
return getRackNumber() + "," + getItemNumber() + "," + getI();
}
}
public static void main(String[] args) {
Item item1, item2, item3;
item1 = new Item("rack1", "item1", 1);
item2 = new Item("rack2", "item2", 12);
item3 = new Item("rack3", "item1", 15);
String ss = "sad";
ArrayList<Item> arrayItem = new ArrayList<>();
arrayItem.add(item1);
arrayItem.add(new Item("rack1","item2", 2));
arrayItem.add(new Item("rack1", "item3", 3));
arrayItem.add(item2);
arrayItem.add(item3);
arrayItem.add(item3);
System.out.println(arrayItem);
for(Iterator<Item> iterator = arrayItem.iterator(); iterator.hasNext(); ) {
Item item = iterator.next();
if (item.getRackNumber().equals("rack1") && item.getItemNumber().equals("item2")) {
iterator.remove(); // Delete rack1 item2
}
}
System.out.println(arrayItem);
}
}
英文:
I have an ArrayList ArrayList<Item>
. I want to delete the item2
from rack1
. But I been stuck here. How can I delete object type of ArrayList with condition?
Expected output will be
[rack1,item1,1, rack1,item3,3, rack2,item2,12, rack3,item1,15, rack3,item1,15]
import java.util.*;
public class Main
{
public static class Item{
private String rackNumber, itemNumber;
private int i;
Item(String rackNumber, String itemNumber, int i){
this.rackNumber = rackNumber;
this.itemNumber = itemNumber;
this.i = i;
}
public String getRackNumber(){return rackNumber;}
public String getItemNumber(){return itemNumber;}
public int getI(){return i;}
@Override
public String toString(){
return getRackNumber()+","+getItemNumber()+","+getI();
}
}
public static void main(String[] args) {
Item item1, item2, item3;
item1 = new Item("rack1", "item1", 1);
item2 = new Item("rack2", "item2", 12);
item3 = new Item("rack3", "item1", 15);
String ss = "sad";
ArrayList<Item> arrayItem = new ArrayList<>();
arrayItem.add(item1);
arrayItem.add(new Item("rack1","item2", 2));
arrayItem.add(new Item("rack1", "item3", 3));
arrayItem.add(item2);
arrayItem.add(item3);
arrayItem.add(item3);
System.out.println(arrayItem);
for(Iterator<Item> iterator = arrayItem.iterator(); iterator.hasNext(); ) {
//Delete rack1 item2
}
System.out.println(arrayItem);
}
}
答案1
得分: 0
调用remove
方法,它会返回布尔值。
英文:
call remove
method, it return boolean
答案2
得分: 0
只需在您的循环中添加以下代码:
Item item = iterator.next();
if ("rack1".equals(item.getRackNumber()) && "item2".equals(item.getItemNumber())) {
iterator.remove();
}
英文:
Just add the codes below in your for loop:
Item item = iterator.next();
if ("rack1".equals(item.getRackNumber()) && "item2".equals(item.getItemNumber()) {
iterator.remove();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论