Class ArrayEnumeration
From Java Example Source Code
Contents |
[edit] Overview - Class ArrayEnumeration
This Java example program introduce using ArrayEnumeration.
[edit] Java Source Code
- Package: example.array
- File: ArrayEnumeration.java
package example.array; import java.lang.reflect.Array; import java.util.Enumeration; public class ArrayEnumeration implements Enumeration { private final int size; private int cursor; private final Object array; public ArrayEnumeration(Object obj) { Class type = obj.getClass(); if (!type.isArray()) { throw new IllegalArgumentException("Invalid type: " + type); } size = Array.getLength(obj); array = obj; } public boolean hasMoreElements() { return (cursor < size); } public Object nextElement() { return Array.get(array, cursor++); } public static void main(String args[]) { Object obj = new int[] { 2, 3, 5, 8, 13, 21 }; ArrayEnumeration e = new ArrayEnumeration(obj); while (e.hasMoreElements()) { System.out.println(e.nextElement()); } try { e = new ArrayEnumeration(ArrayEnumeration.class); } catch (IllegalArgumentException ex) { System.out.println(ex.getMessage()); } } }
[edit] What Result You Can Get
Run the program, you will get:
2 3 5 8 13 21 Invalid type: class java.lang.Class
[edit] Required External Libraries and/or Files for this Java Example
Need nothing.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
