【发布时间】:2020-07-21 04:22:30
【问题描述】:
问题说明了一切。我尝试使用 LinkedHashSet 但没有奏效。如果我没有错,则由于它正在比较整数数组,因此没有正确完成比较。
我的目标是创建一个静态方法 removeDuplicates(ArrayList
import java.util.ArrayList;
import java.util.LinkedHashSet;
public class test {
public static void main(String[] args) {
ArrayList<Integer[]> arrayList = new ArrayList<>();
Integer[] array1 = new Integer[2];
array1[0] = 1;
array1[1] = 4;
Integer[] array2 = new Integer[2];
array2[0] = 1;
array2[1] = 4;
arrayList.add(array1);
arrayList.add(array2);
LinkedHashSet<Integer[]> hashSet = new LinkedHashSet<>(arrayList);
ArrayList<Integer[]> listWithoutDuplicates = new ArrayList<>(hashSet);
System.out.println("Number of elements before using linkedhashset: " + arrayList.size());
System.out.println("number of elements after: (expecting 1) " + listWithoutDuplicates.size());
}
}
给出结果:
Number of elements before using linkedhashset: 2
number of elements after (expecting 1): 2
【问题讨论】:
-
很高兴展示您解决问题的尝试和一些测试用例。
-
@MajedBadawi 到目前为止,我已经用一个基本的测试用例添加了我的尝试
-
有什么要求,你要检查数组内容,它应该保存多少数据,是int only还是object?
-
@silentsudo 我想检查数组内容和顺序。我已经相应地修改了问题
-
是否可以将您的代码从
ArrayList<Integer[]>迁移到ArrayList<List<Integer>>?
标签: java arrays arraylist duplicates