Tony Stark Tony Stark - 1 year ago 66
Java Question

using Enumeration<E> 's hasMoreElements() and nextElement() method to print a list, succeeded 1st time, failed 2nd time after adding some element

I'm new to Java and is trying to learn the Enumeration 's hasMoreElements() and nextElement() method. After I added some elements to the

list while deleted others from the
list, I tried to print everything, however, it failed to do so? Why is this happening?

Properties p1 = new Properties();

try (OutputStream os1 = new FileOutputStream("")) {

//set the properties value
p1.setProperty("database", "localhost");
p1.setProperty("1", "one");
p1.setProperty("2", "two");

} catch (IOException e) {

Enumeration<Object> eString1 = p1.elements();
while (eString1.hasMoreElements()) {

System.out.println("after printing eString1");

p1.setProperty("3", "three");

System.out.println("before printing eString1 again");

while (eString1.hasMoreElements()) {


after printing eString1
before printing eString1 again

ajb ajb
Answer Source

The basic answer is that you cannot rely on Enumeration working if you modify the thing you're enumerating (in this case, the Properties). Once you do that, the Enumeration you constructed may no longer work.

The newer version of Enumeration, Iterator, is the same way, with the exception that some Iterators provide a remove method that allows you to remove the "current element" from a collection without breaking the iterator. (But you have to use the remove method of the Iterator; you can't use any other mechanism for removing an element, or else you could break the iterator.)

The basic reason is that an Enumeration or Iterator contains state information about the entity you're enumerating. If you modify that entity, the state information may no longer be valid. Suppose, for example, that you're iterating over an ArrayList. Most likely, the iterator will contain an integer that's the "current" index of the array. If you then insert something at the front of the ArrayList, the element that is pointed to by that index will become a different element, and next time you call next on the iterator, it will look at the element at that index and return the same element it returned last time. In your case, I'd guess that one piece of state information kept in the Enumeration is a boolean instance variable that says, "Have I reached the end yet?" Once you reach the end of the Properties, that variable will become true. If you then add more properties, the "end-of-the-enumeration" variable will still be true, even though it's effectively wrong at this point.

It simply isn't practical to try to set up Enumerators and Iterators so that they adjust themselves whenever the underlying entity changes. Either a Properties object (or an ArrayList or whatever) would need to keep track of every Enumerator or Iterator created from it, and then go find all of them and adjust them whenever something is done that changes the Properties; or else each Enumerator or Iterator would need a way to query the Properties to ask it, what changes have happened since the last time I checked? Either of those would be very complicated. Consider also that Properties is a hash table, so that when you add a new element, you could add it to any bucket in the hash table. If the Enumerator has already scanned over that bucket, how would it know to go back and scan it again, after you've added a new element? An Enumerator that works that way could be written, I believe, but it would be very inefficient and messy and complicated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download