Home > Java > Javagetting Started > body text

How to implement majority combination merging in java

王林
Release: 2020-08-01 16:38:52
forward
3069 people have browsed it

How to implement majority combination merging in java

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);
	}
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template