当前位置:网站首页>Enhanced for loop

Enhanced for loop

2022-07-04 02:36:00 XGTX1998

1、 Introduce

enhance for The role of circulation : Simplify the writing format of iterators .( Be careful : enhance for The bottom layer of the loop still uses iterator traversal .)
enhance for Scope of application of the cycle : If it is realized Iterable Interface objects or array objects can be enhanced for loop .
enhance for Disadvantages of circulation : enhance for Circulation and iterator The effect of traversal is the same , That is to say, enhance for The inside of the loop is the call iteratoer Realized , But enhance for Circulation has some disadvantages , For example, you can't dynamically delete the contents of a collection in an enhancement loop 、 Can't get subscripts, etc .

2、 enhance for The format of the loop

for( data type Variable name : Traversal target )
{
data type
Variable name : Declare a variable to receive the element after traversal of the traversal target
}

3、 enhance for Circulation considerations

1、 enhance for The bottom layer of the loop is also obtained by using iterators , Just get the iterator by jvm complete , We don't need to get iterators , So we're using enhancements for In the process of looping variable elements, it is not allowed to use set objects to modify the number of elements in the set .
2、 Iterators traverse elements and enhance for The difference between cyclic variable elements : When iterators are used to traverse the elements of a collection, the elements of the collection can be deleted , And enhance for When looping elements of a variable set , Cannot call iterator's remove Method deletion except Elements .
3、 Ordinary for Circulation and enhancement for Difference between cycles : Ordinary for A loop can have no traversal target , And enhance for Loops must have traversal targets .

原网站

版权声明
本文为[XGTX1998]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141818198068.html