我想检查两个arraylist,然后插入到另一个arrayList。但是当我这样做时,我得到了重复的值。如何解决此问题并删除重复项。 我将获取中位数并检查中位数是大于还是小于,然后将值插入第三个数组列表中。
public static void cluster() {
Kmeans kk = new Kmeans();
for (int x = 0; x < cluster1.size() && cluster1 != null; x++) {
for (int y = 0; y < cluster2.size() && cluster2 != null; y++) {
String s1 = cluster1.get(x);
String s2 = cluster2.get(y);
try {
int median = kk.distance(s1, s2);
if (s1.length() > median) {
kmcluster1.add(s1);
kmcluster2.add(s2);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
public static int median(String q, String w) {
int h = q.length();
int h1 = w.length();
int kk = 0;
if (h > h1) {
kk = h - h1;
return kk;
} else kk = h1 - h;
return kk;
}
最佳答案
ArrayList
在设计上允许重复值。如果您想要一个禁止重复的数据结构,请考虑改用 Set
的实例。
关于java - ArrayList 打印重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16283601/