如何在不使用任何内置选项的情况下从第一组中找到第二组中不存在的唯一点集。
public class PointSet {
private Point[] pointArr;
}
public PointSet subtract(PointSet other) {
// return null;
}
因此,我的任务是返回一个新集合,该集合具有第二个集合中不存在的第一个集合元素。初始集应保持不变。
例如,如果我有
@Test
public void pointSetSupportsSubtractingAnotherSet() {
PointSet a = getSet(new Point(1, 1), new Point(1, 2));
PointSet b = getSet(new Point(1, 1), new Point(1, 3));
PointSet remainder = a.subtract(b);
assertThat(a, is(getSet(new Point(1, 1), new Point(1, 2))));
assertThat(remainder, is(getSet(new Point(1, 2))));
}
最佳答案
根据您的代码,使用ArrayList的removeAll进行快速解答。
public class PointSet {
private Point[] pointArr;
public PointSet subtract(PointSet other) {
// return null;
PointSet pointSet = new PointSet();
List<Point> pointArrayList = new ArrayList<Point>(Arrays.asList(pointArr));
pointArrayList.removeAll(Arrays.asList(other));
pointSet.pointArr = pointArrayList.toArray(new Point[pointArrayList.size()]);
return pointSet;
}
}