我需要在Java中连接两个字符串数组。
void f(String[] first, String[] second) {
String[] both = ???
}
哪种方法最简单?
我需要在Java中连接两个字符串数组。
void f(String[] first, String[] second) {
String[] both = ???
}
哪种方法最简单?
当前回答
仅使用Java自己的API:
String[] join(String[]... arrays) {
// calculate size of target array
int size = 0;
for (String[] array : arrays) {
size += array.length;
}
// create list of appropriate size
java.util.List list = new java.util.ArrayList(size);
// add arrays
for (String[] array : arrays) {
list.addAll(java.util.Arrays.asList(array));
}
// create and return final array
return list.toArray(new String[size]);
}
现在,这段代码不是最有效的,但它只依赖于标准的java类,而且很容易理解。它适用于任意数量的String[](甚至是零数组)。
其他回答
这是算盘常用的密码。
String[] a = {"a", "b", "c"};
String[] b = {"1", "2", "3"};
String[] c = N.concat(a, b); // c = ["a", "b", "c", "1", "2", "3"]
// N.concat(...) is null-safety.
a = null;
c = N.concat(a, b); // c = ["1", "2", "3"]
我使用下一个方法使用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})
在Java 8中
public String[] concat(String[] arr1, String[] arr2){
Stream<String> stream1 = Stream.of(arr1);
Stream<String> stream2 = Stream.of(arr2);
Stream<String> stream = Stream.concat(stream1, stream2);
return Arrays.toString(stream.toArray(String[]::new));
}
算法爱好者的另一个答案是:
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]
您可以在两行代码中追加这两个数组。
String[] both = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, both, first.length, second.length);
这是一个快速有效的解决方案,适用于原始类型以及所涉及的两个方法都是重载的。
您应该避免使用涉及ArrayList、流等的解决方案,因为这些解决方案需要分配临时内存,没有任何用处。
对于大型数组,应避免for循环,因为这些循环效率不高。内置方法使用速度极快的块复制函数。