题目描述
请在下面的程序中插入一段代码,这段代码的功能是:将a、b数组中不同的数字保存到一个新的数组c中。
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] a = { 15, 20, 30, 40, 55, 60, 75, 80 }, b = { 15, 20, 40, 80,-5 };
int[] c;
//插入你的代码
Arrays.sort(c); //将新数组排序后输出其中的内容
System.out.println(Arrays.toString(c));
}
}
输出样例
[-5, 30, 55, 60, 75]
package com.study.java8;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
Integer[] a = { 15, 20, 30, 40, 55, 60, 75, 80 }, b = { 15, 20, 40, 80,-5 };
LinkedList<Integer> list = new LinkedList<Integer>();
for (Integer str : a) {
if(!list.contains(str)) {
list.add(str);
}
}
for (Integer str : b) {
if (list.contains(str)) {
list.remove(str);
}else{
list.add(str);
}
}
//差集为
System.out.println(list);
}
}
结果: