I was reading Enumerations in Java, and every example I have come across uses the Vector, and then uses Vector.elements() to get the Enumeration to iterate using Enumeration.nextElement().
My question is how to use the Enumeration interface with my classes, for example if I create a simple class that implements Enumeration like so:
public class Tester implements Enumeration{
private int [] arr = new int[5];
private int iterator = 0;
private int count = 0;
public boolean insert(int item)
{
if (count < 5)
{
arr[count-1] = item;
count++;
return true;
}
else
{
return false;
}
}
public boolean hasMoreElements() {
if (iterator >= 4)
{
return false;
}
else
{
return true;
}
}
public Object nextElement()
{
if (iterator < count)
{
iterator++;
return (Object)arr[iterator -1];
}
else
{
iterator = 0;
return nextElement();
}
}
}
My questions are:
- Whats next? I mean how do I use the Enumeration interface with this
class? What can I do with it (please also explain the purpose of
Enumeration and implementing it in a class)? - How to write the Tester.elements() function similar to
Vector.elements() which is used in the examples over the internet? - What is its equivalent in C#?
Enumeration is a Java 1.0 interface that should not be used. It’s been supplanted by java.util.Iterator. I’d recommend that you prefer that.