我需要在Java中连接两个字符串数组。
void f(String[] first, String[] second) {
String[] both = ???
}
哪种方法最简单?
我需要在Java中连接两个字符串数组。
void f(String[] first, String[] second) {
String[] both = ???
}
哪种方法最简单?
当前回答
我最近一直在与过度的记忆循环作斗争。如果已知a和/或b通常是空的,这里是silvertab代码的另一种修改(也被通用化):
private static <T> T[] concatOrReturnSame(T[] a, T[] b) {
final int alen = a.length;
final int blen = b.length;
if (alen == 0) {
return b;
}
if (blen == 0) {
return a;
}
final T[] result = (T[]) java.lang.reflect.Array.
newInstance(a.getClass().getComponentType(), alen + blen);
System.arraycopy(a, 0, result, 0, alen);
System.arraycopy(b, 0, result, alen, blen);
return result;
}
编辑:这篇文章的前一个版本指出,像这样的数组重用应该清楚地记录下来。正如Maarten在评论中指出的那样,一般来说,最好删除if语句,这样就不需要文档了。但话说回来,那些if语句首先就是这个特定优化的要点。我会在这里留下这个答案,但要小心!
其他回答
允许连接多个阵列的简单变体:
public static String[] join(String[]...arrays) {
final List<String> output = new ArrayList<String>();
for(String[] array : arrays) {
output.addAll(Arrays.asList(array));
}
return output.toArray(new String[output.size()]);
}
我有一个简单的方法。您不想浪费时间研究复杂的java函数或库。但返回类型应该是String。
String[] f(String[] first, String[] second) {
// Variable declaration part
int len1 = first.length;
int len2 = second.length;
int lenNew = len1 + len2;
String[] both = new String[len1+len2];
// For loop to fill the array "both"
for (int i=0 ; i<lenNew ; i++){
if (i<len1) {
both[i] = first[i];
} else {
both[i] = second[i-len1];
}
}
return both;
}
这么简单。。。
算法爱好者的另一个答案是:
public static String[] mergeArrays(String[] array1, String[] array2) {
int totalSize = array1.length + array2.length; // Get total size
String[] merged = new String[totalSize]; // Create new array
// Loop over the total size
for (int i = 0; i < totalSize; i++) {
if (i < array1.length) // If the current position is less than the length of the first array, take value from first array
merged[i] = array1[i]; // Position in first array is the current position
else // If current position is equal or greater than the first array, take value from second array.
merged[i] = array2[i - array1.length]; // Position in second array is current position minus length of first array.
}
return merged;
用法:
String[] array1str = new String[]{"a", "b", "c", "d"};
String[] array2str = new String[]{"e", "f", "g", "h", "i"};
String[] listTotalstr = mergeArrays(array1str, array2str);
System.out.println(Arrays.toString(listTotalstr));
结果:
[a, b, c, d, e, f, g, h, i]
我使用下一个方法使用java8连接任意数量的相同类型的数组:
public static <G> G[] concatenate(IntFunction<G[]> generator, G[] ... arrays) {
int len = arrays.length;
if (len == 0) {
return generator.apply(0);
} else if (len == 1) {
return arrays[0];
}
int pos = 0;
Stream<G> result = Stream.concat(Arrays.stream(arrays[pos]), Arrays.stream(arrays[++pos]));
while (pos < len - 1) {
result = Stream.concat(result, Arrays.stream(arrays[++pos]));
}
return result.toArray(generator);
}
用法:
concatenate(String[]::new, new String[]{"one"}, new String[]{"two"}, new String[]{"three"})
or
concatenate(Integer[]::new, new Integer[]{1}, new Integer[]{2}, new Integer[]{3})
简单一点怎么样
public static class Array {
public static <T> T[] concat(T[]... arrays) {
ArrayList<T> al = new ArrayList<T>();
for (T[] one : arrays)
Collections.addAll(al, one);
return (T[]) al.toArray(arrays[0].clone());
}
}
只需执行Array.concat(arr1,arr2)。只要arr1和arr2是相同类型的,这将为您提供另一个包含这两个数组的相同类型的数组。