Get the App
SLTechnology News&Howtos  ›  Development  › 

How to analyze the overview and use of enhanced for

Shulou Source: shulou.com Published: 2022-06-02 10:28:42 10月04日 Update

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.

Tags: Array element method type length parameter iteration object data analysis content variable tool skill format knowledge static variable storage ordinary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi Shulou Technology Huawei MySQL