The values() method in Java is used to return a constant list of enumeration types. It creates an array containing all the constants of the enumeration type, in the same order as the enumeration is declared. This method helps iterate over enumeration constants, find constants based on strings, create lists or arrays containing constants, and compare constants.
Usage of values() in Java
values()
method in Java Is a static method used to return a constant list of enumeration types. It is part of the java.lang.Enum class and is used to handle enumerated types.
Usage:
<code class="java"><EnumTypeName>.values()</code>
Function:
values()
method creates an enumeration containing An array containing all constants of the enumeration type. The element types of arrays are the same as enumeration types.
Return value:
An array containing all constants of the enumeration type.
Example:
Consider the following enumeration type:
<code class="java">public enum Color { RED, GREEN, BLUE }</code>
To get a list of constants for the Color enumeration type, you can use the following code: ## The #
<code class="java">Color[] colors = Color.values();</code>colors array will now contain the RED, GREEN, and BLUE constants.
Usage scenarios:
values() The method is useful in the following scenarios:
The above is the detailed content of Usage of values in java. For more information, please follow other related articles on the PHP Chinese website!