Presentation is loading. Please wait.

Presentation is loading. Please wait.

More On Enumeration Types

Similar presentations


Presentation on theme: "More On Enumeration Types"— Presentation transcript:

1 More On Enumeration Types
Lecture 10

2 What are enum types? enum types are reference types
an enum type is declared with an enum declaration an enum declaration is a comma-separated list of enum constants an enum declaration may include other components of tradational classes such as constructors fields methods Deitel and Deitel, Java - How to Program,7th Edition, page 288 Show Craps2.java

3 Restrictions on enum types
enum types are implicitly final, because they declare constants that should not be modified enum constants are implicitly static any attempt to create an object of an enum type with operator new results in a compilation error Deitel and Deitel, Java - How to Program,7th Edition, page 288

4 What can you do with them?
“You can use them anywhere constants can be used such as in the case labels for switch statements and to control enhanced for statements”

5 For Statement Review For statement known as a “counted” loop
use when you know how many times you want to loop loop control variable can be declared inside loop in which case its scope is the loop and it is not known outside the loop. It is local to the loop. for (int i = 0; i < 10; i++) loop control variable can be declared outside loop which which case its scope is the block in which it is declared. for (i = 0; i < 10; i++) Show TestInsideForLoop TestOutsideForLoop

6 For Statement Review, continued
Java allows you to do “evil” things with a for loop. However, you, the programmer, should not do any of them. Here are the rules to follow: programmer should not assign values to the loop control variable within the loop programmer should not alter the value of the terminating value of the loop Show TestEvilForLoop

7 Enhanced For Statement
Syntax For (parameter : arrayName) statement; The type of the parameter must be consistent with the type of the elements in the array (i.e. The type specified in the parameter is whatever type of value is stored in the array). The loop selects one value from the array during each iteration. The enhanced for statement iterates through successive values in the array, one by one. Deitel and Deitel, Java - How to Program,7th Edition, page 312 Show TestEnhancedFor and TestBadEnhancedFor

8 EnumSet range public static <E extends Enum<E>> EnumSet<E> range(E from, E to) Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints. The returned set will contain the endpoints themselves, which may be identical but must not be out of order. Parameters: from - the first element in the range to - the last element in the range Returns: an enum set initially containing all of the elements in the range defined by the two specified endpoints Throws: NullPointerException - if first or last are null IllegalArgumentException - if first.compareTo(last) > 0 from API Show DeitelBook and DeitelBookDriver

9

10

11


Download ppt "More On Enumeration Types"

Similar presentations


Ads by Google