The most complete usage summary of for loop, a new feature of Java

1. Overview of enhancements

Enhanced for loop, also known as foreach loop, is used to traverse arrays and containers (collection classes). When using foreach loop to traverse arrays and collection elements, there is no need to obtain the array and collection length, and there is no need to access array and collection elements according to the index, which greatly improves the efficiency and the code is quite concise.

2. Explanation of Oracle official website

So when should you use the for-each loop? Any time you can. It really beautifies your code. Unfortunately,you cannot use it everywhere. Consider,for example,the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator,so you cannot call remove. Therefore,the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally,it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were kNown by the designers,who made a conscIoUs decision to go with a clean,simple construct that would cover the great majority of cases.

So when should you use a for each loop? Anytime. This really beautifies your code. Unfortunately, you can't use it anywhere. Consider these situations, for example, deletion methods. To remove the current element, the program needs to access the iterator. The for each loop hides the iterator, so you can't call the delete function. Therefore, the for each loop is not suitable for filtering elements. Similarly, a loop that needs to replace elements when traversing a collection or array is not applicable. Finally, it is not suitable for parallel loop use in multiple set iterations. Designers should understand these defects and consciously design a clean and simple structure to avoid these situations. If you are interested, you can view the API on the official website. If you don't know how to find the API on the official website, please click open the official website to view the API method.

3. Enhanced for format

Explanation on the official website:

for (TimerTask t : c) t.cancel();

When you see the colon (:) read it as “in.” The loop above reads as “for each TimerTask t in c.” As you can see,the for-each construct combines beautifully with generics. It preserves all of the type safety,while removing the remaining clutter. Because you don't have to declare the iterator,you don't have to provide a generic declaration for it. (The compiler does this for you behind your back,but you need not concern yourself with it.)

To the effect that:

When you see the colon (:), it reads "come in." The above loop reads as "traverse each TimerTask element in C." As you can see, the for each structure is perfectly combined with generics. It preserves all types of security while removing the remaining confusion. Because you don't have to declare an iterator, you don't have to provide a generic declaration for it. The compiler has been done behind your back. You don't need to care about it

Simple experience:

1. Enhanced for traversal array

2. Enhance for traversal collection

4. Enhance the underlying principle

Look at the code first

As can be seen from the above code, the bottom layer is implemented by iterators, and the enhanced for actually hides the iterators, so there is no need to create iterators, and the natural code is much simpler. This is also the reason for the introduction of enhanced for, which is to reduce code, facilitate traversal of sets and arrays, and improve efficiency.

Note: because the enhanced for hides the iterator, when using the enhanced for to traverse the collection and array, you must first judge whether it is null, otherwise a null pointer exception will be thrown. The reason is very simple. The underlying layer needs to call the iterator () method with an array or collection object to create an iterator (iterator iterator is an interface, so it needs to be implemented with Subclasses). If it is null, an exception must be thrown.

5. Enhance the applicability and limitations of for

1. Applicability

It is suitable for traversal of sets and arrays.

2. Limitations:

① Collection cannot be null because the underlying is an iterator.

② The iterator is hidden, so the collection cannot be modified (added or deleted) when traversing the collection.

③ Corner markers cannot be set.

6. Detailed explanation of enhanced for usage

1. Enhance the use of for in the array

2. Enhance the use of for in the collection

3. Perfect combination of generics and enhanced for

Note: it must be perfectly integrated with generics, or you have to manually transform down

1. Enhanced for cannot be used without generic effect

Student class

Test code

If the above code removes the two lines of commented code, the program will report an error, because the collection does not declare what type the element is, and the iterator naturally does not know what type it is. Therefore, if there is no generics, we need to transform downward. We can only use iterators, not enhanced for.

2. Generics and enhancements

Modify the above code generically

4. Four methods of traversing list set

There is an iterator () method in the collection interface, which returns the iterator type, and an iterator is iterator. There is a listiterator () method in the list, so there is an additional collector listiterator. Its subclasses LinkedList, ArrayList and vector all implement the list and collection interfaces, so they can be traversed by two iterators.

Code test

5. Set traversal 2 method

There is no get (int index) method in the set set, so there is no ordinary for loop. There is no listiterator () method in the set, so there is no listiterator iterator. So there are only two ways to traverse.

Code test

7. Summary

1. Enhance the applicability and limitations of for

Applicability: applicable to traversal of sets and arrays.

limitations:

① Collection cannot be null because the underlying is an iterator.

② Corner markers cannot be set.

③ The iterator is hidden, so the collection cannot be modified (added or deleted) when traversing the collection.

2. Enhance the combination of for and generics in the collection to play the role of new features.

3. It's very important to check the new features on the official website. You should know what it is and why it is. You can use it freely only if you feel it in your heart.

The most complete usage summary of the above new Java feature for loop is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>