main函数中倒数第三行调用了removePublic函数,但是结果中并没有删除调用函数后本该删除的B、C中公共元素
public class SJsy {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
SqList A = new SqList(20);
System.out.println("请输入顺序表的长度:");
int n = new Scanner(System.in).nextInt();
SqList.createSqList(A, n);
SqList B= new SqList(20);
System.out.println("请输入顺序表的长度:");
int m = new Scanner(System.in).nextInt();
SqList.createSqList(B, m);
SqList C= new SqList(20);
System.out.println("请输入顺序表的长度:");
int k = new Scanner(System.in).nextInt();
SqList.createSqList(B, k);
SqList.removePublic(A,B,C);
System.out.println("A中各元素的值为:");
A.display();
}
}
static void removePublic(SqList A, SqList B, SqList C) throws Exception {
int i = 0, j = 0, k = 0;
while (j < B.length() && k < C.length()) {
if ((Integer) B.get(j) < (Integer) C.get(k))
j++;
else if ((Integer) B.get(j) > (Integer) C.get(k))
k++;
else {
while (i < A.length() && (Integer) A.get(i) < (Integer) B.get(j))
i++;
if (i < A.length() && Objects.equals((Integer) A.get(i), (Integer) B.get(j)))
A.remove(i);
j++;
k++;
}
}
}
首先,在removePublic方法中输出一下参数值是否存在重复。
其次,debug运行观察removePublic中的逻辑。
应该不是无效,而是你的代码没走进去逻辑。