十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
需求:

現(xiàn)在有多組整數(shù)數(shù)組,需要將他們合并成一個新的數(shù)組。
(推薦教程:java入門教程)
合并規(guī)則:
從每個數(shù)組里按順序取出固定長度的內(nèi)容合并到新的數(shù)組中,取完的內(nèi)容會刪除掉,如果該行不足固定長度或者已經(jīng)為空,則直接取出剩余部分的內(nèi)容放到新的數(shù)組中,繼續(xù)下一行。
(視頻教程推薦:java視頻教程)
代碼實現(xiàn):
package Shuru_lianxi;
import java.util.ArrayList;
import java.util.Scanner;
public class biShi {
public static boolean isNull(ArrayList 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 ma, int num) {
String tem = "";// 作為最后的返回結(jié)果
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);// 刪掉取完的內(nèi)容
} 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] + ",";
}
}
// 將沒取完的數(shù)組重新覆蓋
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 ma = new ArrayList();
sc.nextLine();// nextInt()會留下一個回車,需要消除,否則后邊會出錯
while (!sc.hasNext("#")) {// 以#結(jié)束,這里你可以修改成其他的
ma.add(sc.nextLine());
}
Alg(ma, num);
}
}