0

The following code should work, since join accepts an iterator as argument:

import static org.apache.commons.lang.StringUtils.join;
import java.text.StringCharacterIterator;

…

join(new StringCharacterIterator("A String"), " ");

Alas, it doesn’t. Can somebody explain to me why?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

so the answer is that the Java standard library is a mess. So i need this
@flying sheep: Partly, yes. When you deal with cross-libraries, like Oracle/Java with Apache, it gets even messier.
No. The fact that it ends with Iterator doesn't mean it must have the same interface. A HashTable doesn't have much in common with a WoodenTable.
I don’t want to argue about this. “Iterator” is an unambiguous term in most programming languages (one of which being Java). You just don’t call something an iterator if it doesn’t implement the builtin interface of the same name.
@flying sheep Actually if you look at the javadoc for the Iterator class it talks explicitly about a collection (which an array is not, or there wouldn't be the utility method to convert it to a collection). Also a String is a character array, which as is shown is not a collection in Oracle land. My guess is it is called StringCharacterIterator because CharacterArrayTraversalAgent didn't sound as nice.
|
0

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.

1 Comment

Ah, of course, i forgot to type the second argument. It is in my code, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.