已知字符串数组A,包含初始数据:a1,a2,a3,a4,a5;字符串数组B

已知字符串数组A,包含初始数据:a1,a2,a3,a4,a5;字符串数组B已知字符串数组A,包含初始数据:a1,a2,a3,a4,a5;字符串数组B,包含初始数据:b1,b2,b3,b4,b5。编写程序将数组A、B的每一对应数据项相连接,然后存入字符串数组C,并输出数组C。输出结果为:a1b1,a2b2,a3b3,a4b4,a5b5。
例如:数组A的值为{“Hello ” , “Hello ” , “Hello ” , “Hello ” , “Hello ” },数组B的值为{“Jack” , “Tom” , “Lee” , “John” , “Alisa” },则输出结果为{“Hello Jack” , “Hello Tom” , “Hello Lee” , “Hello John” , “Hello Alisa” }。
要求:
定义2个字符串数组A、B,用于存储初始数据。定义数组C,用于输出结果。
使用循环将数组A、B的对应项相连接,结果存入数组C。(不要边连接边输出)
使用循环将数组C中的值按顺序输出。

public class Foo {
public static void main(String[] args) {
String[] A = { "HELLO ", "HELLO ", "HELLO ", "HELLO ", "HELLO " };
String[] B = { "Jack", "Tom", "Lee", "John", "Alisa" };
String[] C = merge(A, B);
for (int i = 0; i < C.length; i++) {
System.out.print(C[i] + ",");
}
}
public static String[] merge(String[] A, String[] B) {
int length = A.length;
if (A.length > B.length) {
length = B.length;
}
String[] C = new String[length];
for (int i = 0; i < length; i++) {
C[i] = A[i] + B[i];
}
return C;
}
}

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网