我需要在Java中连接两个字符串数组。
void f(String[] first, String[] second) {
String[] both = ???
}
哪种方法最简单?
我需要在Java中连接两个字符串数组。
void f(String[] first, String[] second) {
String[] both = ???
}
哪种方法最简单?
当前回答
我认为泛型的最佳解决方案是:
/* This for non primitive types */
public static <T> T[] concatenate (T[]... elements) {
T[] C = null;
for (T[] element: elements) {
if (element==null) continue;
if (C==null) C = (T[]) Array.newInstance(element.getClass().getComponentType(), element.length);
else C = resizeArray(C, C.length+element.length);
System.arraycopy(element, 0, C, C.length-element.length, element.length);
}
return C;
}
/**
* as far as i know, primitive types do not accept generics
* http://stackoverflow.com/questions/2721546/why-dont-java-generics-support-primitive-types
* for primitive types we could do something like this:
* */
public static int[] concatenate (int[]... elements){
int[] C = null;
for (int[] element: elements) {
if (element==null) continue;
if (C==null) C = new int[element.length];
else C = resizeArray(C, C.length+element.length);
System.arraycopy(element, 0, C, C.length-element.length, element.length);
}
return C;
}
private static <T> T resizeArray (T array, int newSize) {
int oldSize =
java.lang.reflect.Array.getLength(array);
Class elementType =
array.getClass().getComponentType();
Object newArray =
java.lang.reflect.Array.newInstance(
elementType, newSize);
int preserveLength = Math.min(oldSize, newSize);
if (preserveLength > 0)
System.arraycopy(array, 0,
newArray, 0, preserveLength);
return (T) newArray;
}
其他回答
使用流的Java8的另一种方式
public String[] concatString(String[] a, String[] b){
Stream<String> streamA = Arrays.stream(a);
Stream<String> streamB = Arrays.stream(b);
return Stream.concat(streamA, streamB).toArray(String[]::new);
}
在Java 8中使用流:
String[] both = Stream.concat(Arrays.stream(a), Arrays.stream(b))
.toArray(String[]::new);
或者像这样,使用flatMap:
String[] both = Stream.of(a, b).flatMap(Stream::of)
.toArray(String[]::new);
要对泛型类型执行此操作,必须使用反射:
@SuppressWarnings("unchecked")
T[] both = Stream.concat(Arrays.stream(a), Arrays.stream(b)).toArray(
size -> (T[]) Array.newInstance(a.getClass().getComponentType(), size));
算法爱好者的另一个答案是:
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]
我有一个简单的方法。您不想浪费时间研究复杂的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 String[] concat(String[] firstArr,String[] secondArr){
//if both is empty just return
if(firstArr.length==0 && secondArr.length==0)return new String[0];
String[] res = new String[firstArr.length+secondArr.length];
int idxFromFirst=0;
//loop over firstArr, idxFromFirst will be used as starting offset for secondArr
for(int i=0;i<firstArr.length;i++){
res[i] = firstArr[i];
idxFromFirst++;
}
//loop over secondArr, with starting offset idxFromFirst (the offset track from first array)
for(int i=0;i<secondArr.length;i++){
res[idxFromFirst+i]=secondArr[i];
}
return res;
}
就这样,对吧?他没有说他关心订单或任何事情。这应该是最简单的方法。