Why do hasMoreElements
and hasMoreTokens
return the same value?
From the javadoc of hasMoreElements()
/**
* Returns the same value as the hasMoreTokens
* method. It exists so that this class can implement the
* Enumeration interface.
*/
since this is a legacy class which implements Enumeration it also uses the method hasMoreTokens for simplicity method naming.
In simple case which uses just the StringTokenizer you can use the hasMoreTokens method. In polymorphic case which you created or already has more implementation of the Enumeraion interface you will use the hasMoreElements which is part of the Enumeraion interface.
来源:https://stackoverflow.com/questions/6814888/stringtokenizer-method-equivalence