Java enumeration loop traversal and some simple and common usage, java Enumeration

Source: Internet
Author: User

(Reprinted) java enumeration loop traversal and some simple and common usage, java Enumeration

This article reposted from: http://blog.csdn.net/qq_27093465/article/details/51706076 Author: Li xuekai

When do I want to use the enumeration type:

Sometimes, when designing a java model object, you need something such as a constant string, which does not need to be put together with the Global constant of the entire project, it is the most appropriate to put it in the model's java file, so you can have two options:

1. In the java model file, define public final static XXXX = "";

This is a global static variable, which can be directly accessed through the class name.

2. Define public enum XXXX {a, B, c, d, e, f} in the java model file };

When and how to use:

When the constant value needs to be used to compare the data transmitted from the front-end or in the logic operation code, the enumeration type is used.

The general format is: Class Name. Enumeration type name. Single Enumeration type

The above example (Suppose it is in A model java file named ),

It is A. XXXX. a. toString ();

This can be used.

Why do we need to design constants like this:

Here is a code writing principle. This is generally not a human, and no one tells you about it in the book. It's because the code is too much, or you only know the problem when you make a mistake.

In your own code, if you want to make the code very standard,

Then writeThe logic code should not contain constant strings, constant numbers, and other things..

For example, the Code contains numbers, 8,

Or other numbers,

For example, a string contains quotation marks in the logic code.

These codes are hidden bombs even though they are functional.

Good code will not cause this problem. These things should be defined as a constant and then used elsewhere.

Similar to the macro definition in C language.

Otherwise, after a long time, the value of some places suddenly changed, and only one place needs to be modified, so the whole project will not have to worry about problems,

However, if you do not do this, no one knows that you still have such constants in the logic code.

Then the code will have wonderful consequences.

Then, it is blown up.

How to cycle an enumeration type.

There is a method for enumeration, values (),

Usage format: int length = XXXX. values (). length

Returns an array of the same type as the enumerated type.

Then we can loop through this array.

It is the cyclic Enumeration type.

Public enum EnumTest {MON, TUE, WED, THU, FRI, SAT, SUN;} // the output value of the loop for (EnumTest e: EnumTest. values () {System. out. println (e. toString ());}

  

. Ordinal () method.

This method starts from the first enumeration of the enumeration type and increases progressively from scratch.

In the preceding example, a, B, c, d, e, f correspond to numbers, 0, 1, 2, 3, 4, 5 in sequence.

Format: A. XXXX. a. ordinal ();

This method is called.

To create an enumeration type, use the enum keyword,
It implies that all created types are
Java. lang. Enum
Class subclass (java. lang. Enum is an abstract class)
The methods and attributes are as follows:

Introduction to common methods for enum objects


IntCompareTo(E o)
Compare this enumeration with the sequence of the specified object.

Class <E>GetDeclaringClass()
Returns the Class object corresponding to the enumerated type of this enumerated constant.

StringName()
Return the name of this enumerated constant and declare it in its enumerated declaration.

IntOrdinal()
Returns the ordinal number of an enumerated constant (where it is in the enumeration Declaration, where the ordinal number of an initial constant is zero ).

StringToString()
Returns the name of an enumerated constant, which is included in the declaration.

Static <T extends Enum <T> TValueOf(Class <T> enumType, String name)
Returns an enumerated constant of the specified enumeration type with the specified name.

 

In addition, seven methods are provided.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.