【问题标题】:How can I remove an Item from an ArrayList given the itemNum?给定 itemNum,如何从 ArrayList 中删除项目?
【发布时间】:2015-05-23 18:16:40
【问题描述】:

在给定用户将输入的 ItemNo 的情况下,我想删除 Item。如何首先检查是否存在此 itemNo 的项目?然后我将如何删除它?以下是我目前所拥有的,我需要添加一个CompareTo() 方法吗?

物品类别:

public class Item {

public int itemNo;
public String itemName;
public double itemPrice;
public int itemQuantity;

Item(int number, String name, double price, int quantity)
{
itemNo=number;
itemName=name;
itemPrice=price;
itemQuantity=quantity;
}

删除方法:

 public T remove(int givenPosition) {
     T result = null; 
     if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
     assert !isEmpty();
     result = list[givenPosition - 1]; 
     if (givenPosition < numberOfEntries)
                  removeGap(givenPosition);
               numberOfEntries--;
     } // end if
     return result; 

         } // end remove

包含方法:

 public boolean contains(T anEntry) {
     boolean found = false;
     for (int index = 0; !found && (index < numberOfEntries); index++) {
     if (anEntry.equals(list[index])) 
         found = true;
     }//end for
     return found;
     } // end contains

等于方法:

public boolean equals(Object object){
        Item item = (Item) object;
        if(itemNo == item.itemNo)
            return true;
        return false;
    }

【问题讨论】:

  • ArrayList 你的班级还是来自java.util 包的班级?
  • 您应该将答案标记为有帮助。不要让问题悬而未决。与您之前的问题相同。

标签: java object arraylist casting


【解决方案1】:

好吧,你正在尝试分离逻辑,但我会给你代码 sn-p 来解决你的问题。

Item item = new Item(10 , "Item1", 50 , 100);
Item item2 = new Item(20 , "Item2", 50 , 100);
Item item3 = new Item(30 , "Item3", 50 , 100);

List<Item> itemList = new ArrayList<Item>();
itemList.add(item1);
itemList.add(item2);
itemList.add(item3);

public void removeItem(int itemNo){
       Iterator<Item> it = itemList.iterator();
       while(it.hasNext){
         if(item.next().itemNo == itemNo){
            it.remove;
            break;
         }
    }
}

【讨论】:

  • 为什么使用迭代器而不是 for 循环?
  • Iterator for remove 是避免丢失某些行的更好选择,或者您需要以相反的顺序解析列表...
  • 如果你在没有迭代器的循环中尝试添加或删除,你会得到java.util.ConcurrentModificationException
  • @MuratK。只有当我们使用增强型循环时,简单的for(int i...) 才不会抛出 ConcurrentModificationException。但是,是的,使用 Iterator 更安全,因为使用简单的循环我们需要记住移除后的值移位。
  • 哦,谢谢,我暂时停止使用简单循环了^^
【解决方案2】:

从 Java8 开始,任何 Collection(如 List)都有removeIf(Predicate&lt;? super E&gt; filter) 方法,可以删除项目匹配过滤器。所以你的代码看起来像

list.removeIf(item -> item.getItemNo() == x );//where x is value you want to remove

【讨论】:

  • 我喜欢这个答案,很干净。我也不知道这种删除物品的方式。我认为这也适用于 Android?
  • @Chackle 我不确定 Java8 是否已迁移到 android(我不是 android 开发人员,所以无法回答)。
  • Java8 中有一些非常酷的东西,例如这个,还有一些会产生某种魔法代码的东西,例如监听器上的 lambda 符号。我试过了,但我还是喜欢旧的表示法。
  • @MuratK。是的,要习惯 Java 8 中引入的 lambda 和流需要一些时间。旧的表示法很干净,但是新工具使您可以使用并行处理,这可能很好。
【解决方案3】:
private void removeObjectWithNumber(int number) {
    for (int i = 0; i < list.size(); i++) {
        Item item = list.get(i);
        if (item.getItemNo() == number) {
            list.remove(i);
            return;
        }
    }
}

【讨论】:

  • 我想你会发现他的列表实际上并不是一个列表,而是一个数组。措辞不佳的问题。
  • @andybrown 你是对的。我想将我的答案从 list.get(i) 更改为 list[i] 会解决这个问题。但是,必须以另一种方式移除。
  • 这只有在number 是唯一值时才有效。如果不是,那么在删除它之后列表中的元素将被移动,因此如果您将在下一次迭代中转到下一个i++,您将跳过一个元素(您不会对其进行测试)。这意味着如果有两个 Items 和相同的 number 彼此靠近,您将跳过第二个。
  • @pshemo 我知道这一点。该问题说明了如何在给定 itemNum 的情况下删除“一个”项目。该问题几乎假定要为删除方法提供唯一编号。
  • 好的,我只是写它以使其明确,对于 OP 但对于任何读者来说都不是必需的:)
【解决方案4】:

使用数组列表:

添加:

ArrayList<Item> mList = new ArrayList<Item>();
mList.add(new Item(101, "Television", 20000, 4));
mList.add(new Item(102, "Mobile", 6000, 4));

删除:

mList.remove(i /* index */);

包含:

    // override equals method in class
    @Override
  public boolean equals(Object v) {
        boolean retVal = false;

        if (v instanceof A){
            A ptr = (A) v;
            retVal = ptr.itemNo.longValue() == this.itemNo;
            // if you want to check more then you can do
            //  retVal = ptr.itemName.toString().equals(this.itemName.toString());
// further same for itemPrice, itemQuantity
        }

     return retVal;
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 2023-03-15
    • 2013-08-31
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多