pou*_*sma 19 java arrays variadic-functions
我想知道是否有一种简单,优雅和可重用的方法将字符串和字符串数组传递给期望varargs的方法.
/**
* The entry point with a clearly separated list of parameters.
*/
public void separated(String p1, String ... p2) {
merged(p1, p2, "another string", new String[]{"and", "those", "one"});
}
/**
* For instance, this method outputs all the parameters.
*/
public void merged(String ... p) {
// magic trick
}
Run Code Online (Sandbox Code Playgroud)
即使所有类型都是一致的(String)我也找不到告诉JVM 压扁 p2并将其注入合并参数列表的方法?
此时,唯一的方法是创建一个新数组,将所有内容复制到其中并将其传递给该函数.
任何的想法?
根据您的建议,这里是我将使用的通用方法:
/**
* Merge the T and T[] parameters into a new array.
*
* @param type the destination array type
* @param parameters the parameters to merge
* @param <T> Any type
* @return the new holder
*/
@SuppressWarnings("unchecked")
public static <T> T[] normalize(Class<T> type, Object... parameters) {
List<T> flatten = new ArrayList<>();
for (Object p : parameters) {
if (p == null) {
// hum... assume it is a single element
flatten.add(null);
continue;
}
if (type.isInstance(p)) {
flatten.add((T) p);
continue;
}
if (p.getClass().isArray() &&
p.getClass().getComponentType().equals(type)) {
Collections.addAll(flatten, (T[]) p);
} else {
throw new RuntimeException("should be " + type.getName() +
" or " + type.getName() +
"[] but was " + p.getClass());
}
}
return flatten.toArray((T[]) Array.newInstance(type, flatten.size()));
}
normalize(String.class, "1", "2", new String[]{"3", "4", null}, null, "7", "8");
Run Code Online (Sandbox Code Playgroud)
我不认为有一种方法可以将标量和数组隐式展平为单个数组.
我能想到的最干净的解决方案是使用辅助函数:
// generic helper function
public static<T> T[] join(T scalar, T[] arr) {
T[] ret = Arrays.copyOfRange(arr, 0, arr.length + 1);
System.arraycopy(ret, 0, ret, 1, arr.length);
ret[0] = scalar;
return ret;
}
public void separated(String p1, String ... p2) {
merged(join(p1, p2));
}
Run Code Online (Sandbox Code Playgroud)
小智 0
我认为创建一个新数组是正确的方法。
例如,您可以使用ArrayUtilsApache Commons来实现这一目标。
ArrayUtils.addAll(p2, p1);
Run Code Online (Sandbox Code Playgroud)
最好的问候,托马斯
| 归档时间: |
|
| 查看次数: |
6481 次 |
| 最近记录: |