【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>类型返还
---------------------
作者:小轩砸
来源:CSDN
原文:https://blog.csdn.net/m0_37459696/article/details/68939768
版权声明:本文为博主原创文章,转载请附上博文链接!
侵删 联系614506425@qq.com