0

I have 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.

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

But it didn't remove which object have iValue. Because of the resoan static arraylist so, each & every time when i remove size going to change.

Please help me out.

Thanks in advance.


4

3 に答える 3

4

i.remove() を使用する必要があります。http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Iterator.html を参照してください

于 2012-05-18T09:57:54.280 に答える
1

リストでを使用する場合Iteratorは、で現在の要素(リスト内の位置またはインデックスに関して)を安全に削除できますIterator.remove()

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

に同時に変更がある場合、で使用すると問題が発生remove()する可能性があることを考慮してください。引用のjavadoc:static ListListIterator.remove()

このメソッドを呼び出す以外の方法で反復の進行中に基になるコレクションが変更された場合、イテレーターの動作は指定されません。

于 2012-05-18T09:59:50.763 に答える
1

次のコードを使用します。

  for (Iterator<Batch> i = lstString.iterator(); i.hasNext(); ) {
        Batch value=(Batch )i.next();
        if(value.getSequence() == iVal) {
            i.remove();
        }
    }
于 2012-05-18T10:00:30.427 に答える