Java code for Enumeration and using java.lang.reflect.Array

By Saravanan Viewed: 31765 times Emailed: 109 times Printed: 119 times Bookmark and Share



In this tutorial we are going to see how to use Enumeration in java.Enumeration is an interface so we have to implement it from util package. Enumeration specifies two methods. These are, boolean hasMoreElements(), Object nextElement().

Another important class is java.lang.reflect.Array;. eflection is the ability of program to analyze itself. This package provides the ability to obtain information about the fields, constructors, methods and modifiers of a class. And this package class enables you to create and access arrays dynamically.

import java.lang.reflect.Array;
import java.util.Enumeration;

public class EnumExample implements Enumeration
{
private final int i;

private int j;

private final Object o;

public EnumExample(Object obj) 
{
Class classtype = obj.getClass();
if (!classtype.isArray()) 
{
throw new IllegalArgumentException("Invalid type: " + classtype);
}
i = Array.getLength(obj);
o = obj;
}

public boolean hasMoreElements() 
{
return (j < i);
}

public Object nextElement() 
{
return Array.get(o, j++);
}

public static void main(String args[]) 
{
Object obj = new int[] { 1, 2, 7, 9, 18, 27, 36 };
EnumExample e = new EnumExample(obj);
while (e.hasMoreElements()) 
{
System.out.println(e.nextElement());
}
try 
{
e = new EnumExample(EnumExample.class);

catch (IllegalArgumentException ex) 
{
System.out.println(ex.getMessage());
}
}
}

Output:

1
2
7
9
18
27
36
Invalid type: class java.lang.Class




Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2010-09-02]Steps in using verisign certificate with Glassfish appserver
[2010-08-02]emulator 0 terminated while waiting for it to register!
[2010-08-02]Cannot run program "C:\Program Files\Java\jre6\bin\javac.exe": CreateProcess error=2, The system cannot find the file specified
[2010-08-01]Step by Step guide to setup freetts for Java
[2010-07-31]Speech Packages available for Java API
[2010-07-31]Tutorial on setting up freetts with maven
[2010-07-31]package com.sun.speech.freetts does not exist.
[2010-07-31]Text to Speech conversion program in Java
[2010-07-31]How to create wav file using freetts
[2010-07-31]How to set the width of a Text element in JavaFX?
[2010-07-31]Major components of FxObjects in JavaFX
[2010-07-03]Using the AWS SDK for Java in Eclipse
[2010-07-03]Using the AWS SDK for Java
[2010-01-01]Converting properties using PropertyEditors and Other Spring features worth mentioning
[2010-01-01]How to create an array and method in JSP

More Latest News

Most Viewed Articles (in last 30 days)
How to use ArrayList in Java
XML and Java - Parsing XML using Java Tutorial
How to use Iterator in Java
How to Send SMS using Java Program (full code sample included)
Using StringTokenizer in Java
Using substring( ) in Java
FileReader and FileWriter example program in Java
indexOf( ) and lastIndexOf( ) in Java
HashMap example in Java
wait(), notify() and notifyAll() in Java - A tutorial
Abstract classes in Java
compareTo( ) in Java
Method Overriding in Java
instanceof sample program in Java
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
Most Emailed Articles (in last 30 days)
Components of program
How to Send SMS using Java Program (full code sample included)
XML and Java - Parsing XML using Java Tutorial
Why java is important to the Internet
How to use ArrayList in Java
Execute system commands in a Java Program
FileReader and FileWriter example program in Java
Recursion in java
indexOf( ) and lastIndexOf( ) in Java
What is Java?
Method Overloading (function overloading) in Java
compareTo( ) in Java
Sample Java Script that displays a movable clock
History of Object
How to use Iterator in Java