从ArrayList中删除对象 [英] Removing an object from a arraylist

查看:358
本文介绍了从ArrayList中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有姓名,电话号码和位置的ArrayList。

我想从数组中删除某些项目可能的话

反正是有一次,我发现它就像我在下面试图删除的项目?

 公共无效删除(字符串nameToDelete)
{
    对于(输入项:Directory.entries){
        如果(entry.name.equalsIgnoreCase(nameToDelete))
        {
            //删除(输入);
        }
    }
}

感谢


解决方案

理由?

的ArrayList 是返回迭代器快速失败的性质


  

此类的迭代器返回的迭代和的ListIterator 方法故障FAS T:如果列表结构随时修改创建迭代器后以任何方式除非通过迭代器自身的remove或add方法,迭代器都将抛出一个 ConcurrentModificationException的。因此,在并发的修改,迭代器很快就会完全失败,而不是在将来不确定的时间任意冒险,不确定性的行为。


哪里这个迭代器,而我没有使用它来?

有关增强的for循环对集合的Iterator 被使用,所以你不能调用remove方法,而你是迭代。

所以,你的循环下同

 的(迭代器<进入I标记= c.iterator(); i.hasNext();){

解决方法是什么呢?

您可以致电 iterator.remove(); 基于迭代明确,而不是含蓄的变化循环

 字符串inputWord =约翰;
    ArrayList的<串GT;单词表=新的ArrayList<串GT;();
    wordlist.add(第一滴血);
    wordlist.add(约翰);
    对于(的ListIterator<串GT;迭代器= wordlist.listIterator();迭代器
            .hasNext()){
        串Z = iterator.next();
        如果(z.equals(inputWord)){
            iterator.remove();
        }
    }
    的System.out.println(wordlist.size());

现在哪里可以了解更多?


  1. 的for-each循环

  2. ArrayList的Java文档

I have an arraylist with names, phone numbers and locations.

I would like to remove certain items from that array if possible

Is there anyway to remove the item once I have found it like I have tried below?

public void delete(String nameToDelete) 
{    
    for (Entry entry : Directory.entries) { 
        if (entry.name.equalsIgnoreCase(nameToDelete))
        {
            //remove(entry);
        }
    }
}

Thanks

解决方案

Reason?

Iterators returned by ArrayList is fail-fast in nature.

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Where does this iterator Come from while I am not using it?

For enhanced for loop for collections Iterator gets used so you can not call remove method while you are iterating.

So your loop is same as below

for (Iterator<Entry> i = c.iterator(); i.hasNext(); ){   

What is the Solution Then ?

You can call iterator.remove(); and change loop based on iterator explicitly rather than implicitly.

    String inputWord = "john";
    ArrayList<String> wordlist = new ArrayList<String>();
    wordlist.add("rambo");
    wordlist.add("john");
    for (ListIterator<String> iterator = wordlist.listIterator(); iterator
            .hasNext();) {
        String z = iterator.next();
        if (z.equals(inputWord)) {
            iterator.remove();
        }
    }
    System.out.println(wordlist.size());

Now Where Can I Read more?

  1. The For-Each Loop
  2. ArrayList Java docs

这篇关于从ArrayList中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆