简体   繁体   English

Java静态ArrayList <Object> 消除问题

[英]Java static ArrayList<Object> remove issue

I have static ArrayList<Batch> . 我有static ArrayList<Batch> i want to remove which object contain productcode 我想删除包含产品代码的对象

This is my Batch Entity 这是我的批处理实体

public class BatchControl implements Parcelable{
  private String productCode;
  private String lotNumber;
  private String batchSerialNumber;
  private double quantity   ;
  private int sequence;
  private String productName;
  private String type;
  //Setter & getter
 }

This is my removing object 这是我的去除对象

  static ArrayList<String> lstString = new ArrayList<String>();
  for (Iterator<Batch> i = lstString.iterator(); i.hasNext(); ) {
        Batch value=(Batch )i.next();
        if(value.getSequence() == iVal) {
            lstString.remove(value);
        }
    }

iVal is Android TableLayout contain the row number. iVal是Android TableLayout包含的行号。

        String number = txtNumber.getText().toString();
    iVal = Integer.parseInt(number);

But it didn't remove which object have iValue. 但是它并没有删除具有iValue的对象。 Because of the resoan static arraylist so, each & every time when i remove size going to change. 由于resoan静态arraylist如此,每次我删除大小时都会改变。

Please help me out. 请帮帮我。

Thanks in advance. 提前致谢。

When using an Iterator on a list, you can safely remove the current element (regarding position or index in the list) with Iterator.remove() . 在列表上使用Iterator时,可以使用Iterator.remove()安全地删除当前元素(关于列表中的位置或索引Iterator.remove()

for (Iterator<Batch> i = lstString.iterator(); i.hasNext(); ) {
    Batch value=(Batch )i.next();
    if(lstBatCtrl.get(k).getSequence() == iVal) {
        i.remove();
    }
}

Take into account that using remove() on a static List can yield to problems if there are concurrent modifications on the List . 考虑到如果在List上进行并发修改,则在static List上使用remove()会产生问题。 Citing Iterator.remove() 's javadoc: 引用Iterator.remove()的javadoc:

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method 如果在迭代进行过程中以其他方式(而不是通过调用此方法)修改了基础集合,则未指定迭代器的行为

Use the following code: 使用以下代码:

  for (Iterator<Batch> i = lstString.iterator(); i.hasNext(); ) {
        Batch value=(Batch )i.next();
        if(value.getSequence() == iVal) {
            i.remove();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM