Java – the difference between enumeration and enumeration
There is no difference between enumeration data types and enumeration interfaces I became confused between the two
I got our answer. They didn't matter, but they brought me another question
We cannot instantiate the interface So what is the meaning of this line?
Enumeration days = dayNames.elements();
Contains the full code for this line
import java.util.Vector; import java.util.Enumeration; public class EnumerationTester { public static void main(String args[]) { Enumeration days; Vector dayNames = new Vector(); dayNames.add("Sunday"); dayNames.add("Monday"); dayNames.add("Tuesday"); dayNames.add("Wednesday"); dayNames.add("Thursday"); dayNames.add("Friday"); dayNames.add("Saturday"); days = dayNames.elements(); while (days.hasMoreElements()){ System.out.println(days.nextElement()); } } }
Solution
Enumeration is an interface: the object implementing the enumeration interface generates a series of elements, one at a time Consecutive calls to the nexterelement method return a series of consecutive elements
For example, to print all elements of vector < E >, five:
for (Enumeration<E> e = v.elements(); e.hasMoreElements();) System.out.println(e.nextElement());
Enum is a data type: enumeration type is a special data type, which can make variables into a set of predefined constants A variable must be equal to one of its predefined values
For example, you would specify the enumeration type for a week as:
public enum Day { SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY } public static void main(String[] args) { System.out.ptintln(Day.SUNDAY); // print SUNDAY }
Your second question:
Enumeration days = dayNames.elements();
Daynames is a vector, a collection, just like a list There are differences, but this is beyond the scope of the problem However, when calling daynames Elements(), which returns an enumeration of the components of the vector date The enumeration object returned will generate all items added to this vector The first item generated is the item with index 0, the item at index 1, and so on