多个数组内容拼接,每个数组分别根据索引位置,拼接内容
1,通过Hutool.ArrayUtil.zip方法进行处理(缺点,只能保留最短的数组内容)
public class Test {
public static void main(String[] args) {
String chinese = "张三;李四;王五";
String english = "zhangsan;lisi";
String age = "11;";
String[] a1 = chinese.split(";");
String[] a2 = english.split(";");
String[] a3 = age.split(";");
Map zip = ArrayUtil.zip(a1, a2);
System.out.println("zip = " + zip);
}
}
结果
zip = {李四=lisi, 张三=zhangsan}
2,程序逻辑实现(短数组通过填充,解决数组越界)
public class Test {
public static void main(String[] args) {
String chinese = "张三;李四;王五";
String english = "zhangsan;lisi";
String age = "11;";
String[] a1 = chinese.split(";");
String[] a2 = english.split(";");
String[] a3 = age.split(";");
int[] lengthArr = {a1.length, a2.length, a3.length};
int max = ArrayUtil.max(lengthArr);
String[] t1 = transfer(max, a1);
String[] t2 = transfer(max, a2);
String[] t3 = transfer(max, a3);
String[] todo = new String[max];
for (int i = 0; i < max; i++) {
todo[i] = t1[i] + " " + t2[i] + " " + t3[i];
}
System.out.println(Arrays.asList(todo));
}
private static String[] transfer(int max, String[] a1) {
String[] m1 = new String[max];
for (int i = 0; i < m1.length; i++) {
if (a1.length - 1 >= i) {
m1[i] = a1[i];
} else {
m1[i] = "";
}
}
return m1;
}
}
结果
[张三 zhangsan 11, 李四 lisi , 王五 ]