Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the overview and use of enhanced for

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to analyze the overview and use of enhanced for, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Overview and use of enhanced for (foreach)

1. Overview and use of enhanced for (foreach)

Format:

For (element type variable name in array or Collection collection: array or Collection collection object) {

/ / just use the variable name directly

}

Note: the underlying layer of enhanced for is implemented by iterators, do not use enhanced for while changing the structure with collections, concurrent modification exceptions will be reported.

2. ArrayList stores custom objects and traverses the enhanced for version

3. Whether the three iterations can be deleted

a. Ordinary for forward traversal can be deleted, but it should be noted that once the element is deleted, the index should be subtracted by 1. 5%.

For (int I = 0; I < list.size ();) {

If ("b" .equals (s)) {

List.remove (I)

Continue

}

ITunes +

}

b. The iterator traverses and can be deleted, but be careful to use the iterator's delete method.

c. Enhanced for traversal time, elements cannot be deleted.

4. Overview and use of static import

For static methods, import to the level of the method

Format:

Import static package name. Class name. Method name

5. Overview and use of variable parameters

It is suitable for cases where the type of parameter is clear but the number of parameters is uncertain.

Note: there are multiple parameters in the method, so the variable parameters must be placed at the end.

6. The use of the asList () method of the Arrays tool class

Convert arrays to collections;-- functions of the Arrays utility class

Public static List asList (T... a)

The size is fixed after being converted to a collection, and add or delete operations are not supported.

Reference data type array-treats the elements in the array as elements in the collection.

Basic data type array-an array object is added to the collection as an element.

Set rotation array:-- Collection

Object [] toArray ()-- convert the collection to Object []

T [] toArray (T [] a)-an array that converts a collection to a specified data type (must be an element type in the collection, otherwise an exception is reported)

If the length of the array is less than or equal to the set, then the length of the returned array is the length of the collection

If the array length is larger than the collection, the returned array length is the specified length, with the elements in the collection saved first and the null stored later.

The above is how to analyze the overview and use of enhanced for. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report