请参阅下面的评论。
public static String [] mergeStrings(String [] x, String [] y) {
//You declared the array that has enough size to wraph both x and y arrays. Good.
String[] result = new String[x.length + y.length];
// get the size of the bigger array?
int largest = (x.length > y.length) ? x.length : y.length;
//If x.length is bigger, this will iterate x.length times, y.length otherwise..
for (int i = 0; i < largest; i++){
//even if you used result[i]=x[i] you would be missing elements for y.
}
}
我认为您正在尝试做的是这样的事情:
public static String[] mergeStrings(String[] x, String[] y) {
//Declare the result string with the sum of sizes.
String[] result = new String[x.length + y.length];
//Assign `x` values to result.
for (int i = 0; i < x.length; i++) {
result[i] = x[i];
}
//Assign `y` values to result. Using resultIndex to start on the first empty position while `i` will be the index for the `y` array.
for (int i = 0, resultIndex = x.length; i < y.length; i++, resultIndex++) {
result[resultIndex] = y[i];
}
return result;
}