1. 程式人生 > >【java】查重類的實現

【java】查重類的實現

最近在學校做大創專案,用到查重機制,寫了一個查重類

附上程式碼:

import java.util.Vector;

public class ElementCheck {

	// 重複優先
	static Vector<Integer> CheckSameElement1(Vector<String> test) {
		Vector<Integer> markA = new Vector();
		Vector<Integer> markB = new Vector();
		for (int i = 0; i < test.size(); i++) {
			markA.add(0);
		}
		for (int i = 0; i < test.size(); i++) {
			if (markA.get(i) != 1) {
				for (int n = i + 1; n < test.size(); n++) {
					if (markA.get(n) != 1) {
						try {
							if (test.get(i).equals(test.get(n))) {
								if (markA.get(i) != 1) {
									markB.add(i);
								}
								markA.set(n, 1);
								markA.set(i, 1);
								markB.add(n);
							}
						} catch (Exception e) {
						}
					}
				}
			}
		}

		return markB;
	}

	// 序號排列優先
	static Vector<Integer> CheckSameElement2(Vector<String> test) {
		Vector<Integer> markA = new Vector();
		Vector<Integer> markB = new Vector();
		for (int i = 0; i < test.size(); i++) {
			markA.add(0);
		}
		int a = 0;
		for (int i = 0; i < test.size(); i++) {
			if (markA.get(i) != 1) {
				for (int n = i + 1; n < test.size(); n++) {
					if (markA.get(n) != 1) {
						try {
							if (test.get(i).equals(test.get(n))) {
								markA.set(n, 1);
								markA.set(i, 1);
							}
						} catch (Exception e) {
						}
					}
				}
			}
		}
		for (int i = 0; i < test.size(); i++) {
			if (markA.get(i) == 1) {
				markB.add(i);
			}
		}
		return markB;
	}

	static Vector<Integer> CheckDifferentElement(Vector<String> test) {
		Vector<Integer> markA = new Vector();
		Vector<Integer> markB = new Vector();
		for (int i = 0; i < test.size(); i++) {
			markA.add(0);
		}
		int a = 0;
		for (int i = 0; i < test.size(); i++) {
			if (markA.get(i) != 1) {
				for (int n = i + 1; n < test.size(); n++) {
					if (markA.get(n) != 1) {
						try {
							if (test.get(i).equals(test.get(n))) {
								markA.set(n, 1);
								markA.set(i, 1);
							}
						} catch (Exception e) {
						}
					}
				}
			}
		}
		for (int i = 0; i < test.size(); i++) {
			if (markA.get(i) != 1) {
				markB.add(i);
			}
		}
		return markB;
	}
}


類及方法的說明:

類:ElementCheck

CheckSameElement1(Vector<String> test)

Vector<String> test:目標陣列

將目標陣列中重複的元素的序號以Vector<Integer>型別返還,排序方式為重複優先

CheckSameElement2(Vector<String> test)

Vector<String> test:目標陣列

將目標陣列中重複的元素的序號以Vector<Integer>型別返還,排序方式為序號排列優先

CheckDifferentElement(Vector<String> test)

Vector<String> test:目標陣列

將目標陣列中不重複的元素的序號以Vector<Integer>型別返還