加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

每个循环工作的Java

发布时间:2020-12-15 04:14:42 所属栏目:Java 来源:网络整理
导读:我正在处理某项任务,当我偶然做错了但是代码执行并提供了正确的结果.我有点惊讶,并且记住了每个循环的所有这些是如何工作的. 示例(示例程序), public static void main( String[] args ){ String myInput = "hello,hi,how are you "; String[] splitted = my
我正在处理某项任务,当我偶然做错了但是代码执行并提供了正确的结果.我有点惊讶,并且记住了每个循环的所有这些是如何工作的.
示例(示例程序),

public static void main( String[] args )
{
    String myInput = "hello,hi,how are you ";
    String[] splitted = myInput.split(",");
    List<String> mylist = new ArrayList<String>();
    for (String output : splitted) 
    {
        mylist.add(output);
    }


    for (String output : mylist) 
    {
        System.out.println(output);
        mylist = new ArrayList<String>(); //It worked 
        mylist.add(output);
    }

    for (String output : splitted) 
    {
        mylist.add(output);
    }

    for (String output : mylist) 
    {
        System.out.println(output);             
        mylist.add(output); // After this line it threw exception java.util.ConcurrentModificationException
    }

}

我很想知道,在搜索的过程中,我发现另外一篇文章说如果我们使用迭代器方法我们可以从列表中删除元素,所以我试过,

for (String output : splitted) 
{
    mylist.add(output);
}
for (Iterator iterator2 = mylist.iterator(); iterator2.hasNext();)
{
    String string = (String) iterator2.next();
    System.out.println(string);
    iterator2.remove(); //It worked but if I used the same thing to remove element from original list it threw exception.
}

现在我只想知道上面引用的每个循环背后发生了什么.
我想知道技术方??面,我知道我无法修改每个循环的集合,但在某些情况下,上面说明了它的工作原理?

解决方法

Now I just want to know what is happening behind the every for each
loop quoted above

1. for (String output : splitted) 
    {
        mylist.add(output);
    }

这会将分割数组中的每个输出字符串添加到mylist列表中.

2. for (String output : mylist) 
{
      System.out.println(output);
      mylist = new ArrayList<String>(); //It worked 
      mylist.add(output);
}

for语句由以下产品管理:

for ( FormalParameter : Expression )
            Statement

其中Expression必须是java.lang.Iterable或数组的实例.所以这个:每个循环都等同于:

Iterator<String> iterator = mylist.iterator();
while (iterator.hasNext()) {
    System.out.println(output);
    mylist = new ArrayList<String>(); //It worked 
    mylist.add(output);
}

这里mylist.iterator()将返回Iterator类型的新实例:

public Iterator<E> iterator() {
        return new Itr();
}

因此,即使您正在创建新的ArrayList实例并在每次迭代时将它们分配给mylist,从原始mylist获取的迭代器仍将具有对原始mylist的引用,并将继续迭代原始mylist的元素.迭代器保持对其创建的列表的引用.赋值mylist = new ArrayList< String>()对迭代器处理的数据没有影响,因为它更改了变量mylist而不是列表本身.

3. for (String output : mylist) 
    {
        System.out.println(output);             
        mylist.add(output); // After this line it threw exception java.util.ConcurrentModificationException
    }

以下语句解释了这种行为.它是从Arraylist doc复制的:

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.

4. for (Iterator iterator2 = mylist.iterator(); iterator2.hasNext();)
{
    String string = (String) iterator2.next();
    System.out.println(string);
    iterator2.remove(); //It worked but if I used the same thing to remove element from original list it threw exception.
}

上面的语句还解释了这个for循环的行为:迭代遍历列表时,迭代器自己的remove或add方法可以在结构上修改列表.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读