注释应该都写的很明白。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int m = sc.nextInt();
if(n == 0 && m == 0) break;
int[] nn = new int[n];
int[] mm = new int[m];
int[] nm = new int[n+m];
//存值
for (int i = 0; i < n; i++) {
nn[i] = sc.nextInt();
}
for (int i = 0; i < m; i++) {
mm[i] = sc.nextInt();
}
//集合相减
int cnt = 0;
for (int i = 0; i < n; i++) {
boolean flat = false;
for (int j = 0; j < m; j++) {
if(nn[i] == mm[j]) flat = true;
}
if(flat == false) {
nm[cnt] = nn[i];
cnt++;
}
}
if(cnt == 0) System.out.println("NULL");
else {
Arrays.sort(nm,0,cnt);
for (int i = 0; i < cnt; i++) {
System.out.print(nm[i]+" ");
}
System.out.println();
}
}
}
}