Requirements:
There are multiple sets of integer arrays and they need to be merged into a new array.
(Recommended tutorial: java introductory tutorial)
Merge rules:
Take out the fixed-length content in order from each array and merge it into a new one In the array, the retrieved content will be deleted. If the row is less than the fixed length or is empty, the remaining content will be directly retrieved and placed into a new array, and the next row will be continued.
(Video tutorial recommendation: java video tutorial)
Code implementation:
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); } }
The above is the detailed content of How to implement majority combination merging in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools