Java如何根据条件删除对象类型的ArrayList。

huangapple go评论69阅读模式
英文:

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&lt;Item&gt;. 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()+&quot;,&quot;+getItemNumber()+&quot;,&quot;+getI();
}
}
public static void main(String[] args) {
Item item1, item2, item3;
item1 = new Item(&quot;rack1&quot;, &quot;item1&quot;, 1);
item2 = new Item(&quot;rack2&quot;, &quot;item2&quot;, 12);
item3 = new Item(&quot;rack3&quot;, &quot;item1&quot;, 15);
String ss = &quot;sad&quot;;
ArrayList&lt;Item&gt; arrayItem = new ArrayList&lt;&gt;();
arrayItem.add(item1);
arrayItem.add(new Item(&quot;rack1&quot;,&quot;item2&quot;, 2));
arrayItem.add(new Item(&quot;rack1&quot;, &quot;item3&quot;, 3));
arrayItem.add(item2);
arrayItem.add(item3);
arrayItem.add(item3);
System.out.println(arrayItem);
for(Iterator&lt;Item&gt; 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 (&quot;rack1&quot;.equals(item.getRackNumber()) &amp;&amp; &quot;item2&quot;.equals(item.getItemNumber()) {
iterator.remove();
}

huangapple
  • 本文由 发表于 2020年10月22日 18:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64480493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定