java 为什么我不能在列表上使用 Apache 的 StringUtils.join?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7164910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:54:54  来源:igfitidea点击:

Why can't I use Apache's StringUtils.join on a List?

javacollectionsapache-commonsapache-stringutils

提问by Eric Wilson

When I try

当我尝试

StringUtils.join(myList,',');

I get a compilation failure:

我得到一个编译失败:

cannot find symbol
symbol  : method join(java.util.List,char)

But the following works:

但以下工作:

StringUtils.join(myList.toArray(),',');

The docs (Apache Commons Lang 2.5)seem to indicate that both should work, as they record both:

文档(阿帕奇共享郎2.5)似乎表明,两者都应该工作,因为它们都记录:

public static String join(Collection collection,
                      char separator)

and

public static String join(Object[] array,
                      char separator)

Any ideas? For the record, I'm importing import org.apache.commons.lang.StringUtils;

有任何想法吗?为了记录,我正在导入import org.apache.commons.lang.StringUtils;

回答by nfechner

The most probable reason is, that you are using an older version of Commons Lang, since the method using a Collectionhas only been added in 2.3.

最可能的原因是,您使用的是旧版本的 Commons Lang,因为使用 a 的方法Collection仅在 2.3 中添加。

You can check that by looking in the MANIFEST.MFfile in the Jar at the Implementation-Versionfield.

您可以通过查看MANIFEST.MFImplementation-Version字段中Jar中的文件来检查。

回答by Phoebe Chan

I had the problem earlier and realized it is due to the order of my import.

我之前遇到过这个问题,并意识到这是由于我的导入顺序造成的。

Once I shifted my commons JAR up the order of import, it works.

一旦我将公共 JAR 按导入顺序向上移动,它就可以工作了。

Hope this helps.

希望这可以帮助。

回答by djechlin

Not quite your problem but related:

不完全是你的问题,但相关:

In org.apache.commons.lang.StringUtils, there exists a method

org.apache.commons.lang.StringUtils,存在一个方法

join(Object[])

That doesn't take a delimiter.

这不需要分隔符。

join(Object[], char)
join(Collection, char)

All take delimiters (may use Stringinstesad of char). So if you forget the delimiter, your error message may be pointing to the wrong problem.

全部采用分隔符(可以使用String代替char)。因此,如果您忘记了分隔符,您的错误消息可能指向了错误的问题。