Perché non può stringerels.join accettare un filo di cordeatterrator?
-
14-11-2019 - |
Domanda
Il seguente codice dovrebbe funzionare, poiché join
accetta un iteratore come argomento:
import static org.apache.commons.lang.StringUtils.join;
import java.text.StringCharacterIterator;
…
join(new StringCharacterIterator("A String"), " ");
.
Ahimè, no.Qualcuno può spiegarmi perché?
Soluzione
According to the documentation, StringCharacterIterator
inherits from and only from CharacterIterator
. A CharacterIterator
does not inherit from a generic Iterator
, which is what join
wants.
http://download.oracle.com/javase/1.4.2/docs/api/java/text/CharacterIterator.html
Altri suggerimenti
First of all, the join method takes two arguments : an iterator and a separator string or char.
And if you look at the javadoc of StringCharacterIterator, you'll see that this class does not implement java.util.Iterator
.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow