Java枚举vs迭代vs ForLoop [英] Java Enumeration vs Iteration vs ForLoop

查看:68
本文介绍了Java枚举vs迭代vs ForLoop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(枚举,迭代器,循环)有什么区别?

What is the difference between (Enumeration, Iterator, Loop) ??

在下面的代码中,每个示例(枚举,迭代器,循环)都获得相同的输出] 但是我需要知道它们之间的真正区别是什么?在特定情况下,我应该使用其中一个而不能使用其他吗?其中一个相对于其他有什么优势吗?

In the following code each example (Enumeration, Iterator, Loop) gets the same output ] but I need to know what is the real difference between them? Is there specific case where i should use one of them and cant use the others? any advantages in one of them over the others?

推荐答案

枚举VS迭代器

迭代器主要与非线程安全集合一起使用.迭代器的设计方式如下:在对集合进行并发修改时它会失败,即如果在多线程环境中使用了为单线程环境创建的集合,并且线程在更改时对其进行了更改,正在遍历,然后遍历失败.

Iterator is mainly used with non Thread safe Collection. Iterator is designed in such a way that it fails whenever concurrent modification are made to the Collections i.e if a collection which is made for single Threaded environment is used in multithreaded environment and changes are made to it by Thread while it is being traversed then the traversing fails.

枚举仅与线程安全集合一起使用.在多线程环境中使用此类集合时,可以确保数据的一致性,因此枚举永远不会失败

Enumeration is used only with Thread safe Collection. When such a collection is used in multithreaded environment consistency of the data is guarantee hence Enumeration never fails

为了更好地理解,您应该使用它们的内部工作方式........

For better understanding you should their Internal Working........

Collection及其迭代器和枚举之间具有更强的HAS-A关系,即双方的存在都相互依赖.

A Collection and its Iterator and Enumeration has a stronger HAS-A relation between them i.e Existence of both parties depends upon each other.

这篇关于Java枚举vs迭代vs ForLoop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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