0

So I am currently upgrading some old code which uses Vector into ArrayList and am unsure what to use to replace the old vector segment.

public Enumeration enumerateMapping() {
    return mappingList.elements();
}

I need to replace the .elements() with an ArrayList equivalent. If anyone happens to know of a link with the translation for these that would be very helpful as well.

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
  • mappingList is a Vector type, by the way. – Gilgaustus Aug 08 '16 at 14:16
  • 4
    Don't comment on your own question to add information; instead [edit] your question to make it better. – azurefrog Aug 08 '16 at 14:17
  • You need to change method signature itself. You cannot replace `.elements()` with `ArrayList` equivalent with out changing return value – SomeDude Aug 08 '16 at 14:18
  • `return Collections.enumeration(mappingList);`You should also give your Enumeration a generic type. – Zircon Aug 08 '16 at 14:18
  • You might consider using the Java 5 "for each" loop, rather than an Enumerator.... [Oracle's site](http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html) – Hatley Aug 08 '16 at 14:19

1 Answers1

1

ArrayList (and most Collections) implement Iterable, so you should return iterator() :

public Iterator enumerateMapping() {
    return mappingList.iterator();
}

It would be better to use the generic type instead of the raw type, i.e. Iterator<ElementType>.

As the Enumeration Javadoc suggests, it is recommended to use Iterator instead of Enumeration :

NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.

Eran
  • 387,369
  • 54
  • 702
  • 768