为什么 StringUtils.join 不能接受 StringCharacterIterator?
下面的代码应该可以工作,因为 join
接受迭代器作为参数:
import static org.apache.commons.lang.StringUtils.join;
import java.text.StringCharacterIterator;
…
join(new StringCharacterIterator("A String"), " ");
唉,事实并非如此。有人可以向我解释为什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据文档,
StringCharacterIterator
继承自且仅继承于CharacterIterator
。CharacterIterator
并不继承自通用Iterator
,而这正是join
想要的。http://download.oracle.com/javase /1.4.2/docs/api/java/text/CharacterIterator.html
According to the documentation,
StringCharacterIterator
inherits from and only fromCharacterIterator
. ACharacterIterator
does not inherit from a genericIterator
, which is whatjoin
wants.http://download.oracle.com/javase/1.4.2/docs/api/java/text/CharacterIterator.html
首先, join 方法有两个参数:一个迭代器和一个分隔符字符串或字符。
如果您查看 StringCharacterIterator 的 javadoc ,您将看到此类没有实现
java.util.Iterator
。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
.