题目18:数组合并
现在有多组整数数组
需要将他们合并成一个新的数组
合并规则从每个数组里按顺序取出固定长度的内容
合并到新的数组
取完的内容会删除掉
如果改行不足固定长度,或者已经为空
则直接取出剩余部分的内容放到新的数组中继续下一行
输入描述
第一 行每次读取的固定长度
长度0<len<10
第二行是整数数组的数目
数目 0<num<10000
第3~n行是需要合并的数组
不同的数组用换行分割
元素之间用逗号分割
最大不超过100个元素
输出描述
输出一个新的数组,用逗号分割
示例1
输入
3
2
2,5,6,7,9,5,7
1,7,4,3,4
输出
2,5,6,1,7,4,7,9,5,3,4,7说明 获得长度3和数组数目2
先遍历第一行 获得2,5,6
再遍历第二行 获得1,7,4
再循环回到第一行获得7,9,5
再遍历第二行获得3,4
再回到第一行获得7示例2
输入
4
3
1,2,3,4,5,6
1,2,3
1,2,3,4
输出
1,2,3,4,1,2,3,1,2,3,4,5,6
private static void addNum() {
Scanner scanner = new Scanner(System.in);
int len = Integer.valueOf(scanner.nextLine());
int addCount = Integer.valueOf(scanner.nextLine());
List<ArrayList<String>> list = new ArrayList<>();
int maxCount = 0;
for (int i = 0; i < addCount; i++) {
String[] split = scanner.nextLine().split(",");
maxCount += split.length;
ArrayList<String> flag = new ArrayList<>(split.length);
Collections.addAll(flag, split);
list.add(flag);
}
List<String> res = new ArrayList<>();
//第二步 数组拼接后放入 res
while (maxCount != res.size()) {
for (ArrayList<String> strlist : list) {
if (0 == strlist.size()) continue;
int times = Math.min(strlist.size(), len); //选择取数次数
for (int i = 0; i < times; i++) {
res.add(strlist.remove(0)); //从原有数据删除+赋值给 res
}
}
}
for (int i = 0; i < res.size(); i++) {
if (i==res.size()-1){
System.out.print(res.get(i));
}else {
System.out.print(res.get(i)+",");
}
}
}