0

Good evening, consider the following:

public class TestEnum implements Enumeration<String> {
private Enumeration<String> files;

private TestEnum(Vector<String> files) {
    this.files = files.elements();
}

public Enumeration<String> getFiles() {
    return files;
}

@Override
public boolean hasMoreElements() {
    return files.hasMoreElements();
}

@Override
public String nextElement() {
    return files.nextElement();
}

public static void main(String[] args) {
    Vector<String> vector = new Vector<>();
    vector.add("1");
    vector.add("2");
    vector.add("3");
    vector.add("4");
    vector.add("5");

    TestEnum obj = new TestEnum(vector);

    while(obj.getFiles().hasMoreElements()) {
        System.out.println(obj.getFiles().nextElement());
    }


}

}

I don't understand, where is nextElement() and hasMoreElements() methods default implementation when operating whit enumeration of strings? I know that methods implementation should be created on programmers own and it have been created , but in line:

return files.nextElement();

I call the method nextElement() on "files" object that has another implementation? If method has my implementation then it should be call nextElement() indefinitely? Or I'm wrong?

Maksim
  • 351
  • 1
  • 2
  • 12
  • 1
    In your constructor you initialize `files` to be whatever implementation of enumeration `Vector` gives you. – azurefrog Sep 08 '17 at 21:26

1 Answers1

0

Sorry, guys, I have found it:

public Enumeration<E> elements() {
    return new Enumeration<E>() {
        int count = 0;

        public boolean hasMoreElements() {
            return count < elementCount;
        }

        public E nextElement() {
            synchronized (Vector.this) {
                if (count < elementCount) {
                    return elementData(count++);
                }
            }
            throw new NoSuchElementException("Vector Enumeration");
        }
    };
}

In Vector class

Maksim
  • 351
  • 1
  • 2
  • 12
  • It is also available in the java.util [package](https://docs.oracle.com/javase/7/docs/api/java/util/Enumeration.html) as **Enumeration** Interface – MaxZoom Sep 08 '17 at 22:15