java 如何在java中将字符串集合转换为字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16801102/
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
How to convert collection of strings to string array in java
提问by Kenogu Labz
I am trying to pass attributeNames
which is a collection of string
to a method setColumnNames
that accepts string array
.
我试图将attributeNames
which is a传递collection of string
给setColumnNames
接受string array
.
public Map<String, String> getAttributes(String rowKey, Collection<String> attributeNames, String columnFamily) {
try {
SliceQuery<String, String, String> query = HFactory.createSliceQuery(keyspace, StringSerializer.get(), StringSerializer.get(), StringSerializer.get()).
setKey(rowKey).setColumnFamily(columnFamily).setColumnNames(attributeNames);
} catch (HectorException e) {
LOG.error("Exception in CassandraHectorClient::getAttributes " +e+ ", RowKey = " +rowKey+ ", Attribute Names = " +attributeNames);
}
return null;
}
Definition for setColumnNames
定义为 setColumnNames
SliceQuery<K, N, V> setColumnNames(N... columnNames);
I am getting this exception everytime-
我每次都会遇到这个异常-
The method setColumnNames(String...) in the type SliceQuery<String,String,String> is not applicable for the arguments (Collection<String>)
How can I convert collection of strings to string array in Java? Any idea?
如何在 Java 中将字符串集合转换为字符串数组?任何的想法?
回答by Kenogu Labz
From Collectionin the Javadocs:
来自Javadocs 中的Collection:
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.
<T> T[] toArray(T[] a)
返回一个包含此集合中所有元素的数组;返回数组的运行时类型是指定数组的类型。如果集合适合指定的数组,则在其中返回。否则,将使用指定数组的运行时类型和此集合的大小分配一个新数组。
Note that this method accepts an array of the same type as the collection; Java does not permit you to instantiate a generic array, though, so you'll need to work around it. For more info, see this question.
请注意,此方法接受与集合类型相同的数组;但是,Java 不允许您实例化通用数组,因此您需要解决它。有关更多信息,请参阅此问题。