您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这期内容当中小编将会给大家带来有关如何实现java多数组合并,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
需求:
现在有多组整数数组,需要将他们合并成一个新的数组。
合并规则:
从每个数组里按顺序取出固定长度的内容合并到新的数组中,取完的内容会删除掉,如果该行不足固定长度或者已经为空,则直接取出剩余部分的内容放到新的数组中,继续下一行。
代码实现:
package Shuru_lianxi;
import java.util.ArrayList;
import java.util.Scanner;
public class biShi {
public static boolean isNull(ArrayList<String> gh) {
int i = 0;
for (i = 0; i < gh.size(); i++) {
if (gh.get(i) != null)
break;
}
if (i < gh.size()) {
return false;
} else {
return true;
}
}
public static void Alg(ArrayList<String> ma, int num) {
String tem = "";// 作为最后的返回结果
while (!isNull(ma)) {
for (int i = 0; i < ma.size(); i++) {
String sk = ma.get(i);
if (sk == null) {
continue;
}
String[] gg = sk.split(",");
if (sk.length() == 0) {
ma.set(i, null);// 删掉取完的内容
} else {
if (gg.length <= num) {
tem = tem + sk + ",";
ma.set(i, null);
} else {
for (int k = 0; k < num; k++) {
tem = tem + gg[k] + ",";
}
String hh = "";
for (int l = num; l < gg.length; l++) {
if (l == gg.length - 1) {
hh = hh + gg[l];
} else {
hh = hh + gg[l] + ",";
}
}
// 将没取完的数组重新覆盖
ma.set(i, hh);
}
}
}
}
System.out.println(tem.substring(0, tem.length() - 1));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
ArrayList<String> ma = new ArrayList<String>();
sc.nextLine();// nextInt()会留下一个回车,需要消除,否则后边会出错
while (!sc.hasNext("#")) {// 以#结束,这里你可以修改成其他的
ma.add(sc.nextLine());
}
Alg(ma, num);
}
}上述就是小编为大家分享的如何实现java多数组合并了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。