Convert String Array into Enumeration

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Convert String Array into Enumeration

This Java example program shows how to convert String Array into Enumeration.

[edit] Java Source Code

  • Package: example.array
  • File: ArrayEnumerationFactory.java
package example.array;
 
import java.lang.reflect.Array;
import java.util.Enumeration;
 
final public class ArrayEnumerationFactory {
    static public Enumeration makeEnumeration(final Object obj) {
	Class type = obj.getClass();
	if (!type.isArray()) {
	    throw new IllegalArgumentException(obj.getClass().toString());
	} else {
	    return (new Enumeration() {
		int size = Array.getLength(obj);
 
		int cursor;
 
		public boolean hasMoreElements() {
		    return (cursor < size);
		}
 
		public Object nextElement() {
		    return Array.get(obj, cursor++);
		}
	    });
	}
    }
 
    public static void main(String args[]) {
	Enumeration e = makeEnumeration(args);
	while (e.hasMoreElements()) {
	    System.out.println(e.nextElement());
	}
	e = makeEnumeration(new int[] { 1, 3, 4, 5 });
	while (e.hasMoreElements()) {
	    System.out.println(e.nextElement());
	}
	try {
	    e = makeEnumeration(new Double(Math.PI));
	} catch (IllegalArgumentException ex) {
	    System.err.println("Can't enumerate that: " + ex.getMessage());
	}
    }
}

[edit] What Result You Can Get

Coming soon...

[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.


Personal tools