假如 a[1,2,3,4,5], b[1,2,3,4] 怎么取得两个数组中不相同的部分 就如怎么取得 5
public static <T> List<T> compare(T[] t1, T[] t2) {
List<T> list1 = Arrays.asList(t1);
List<T> list2 = new ArrayList<T>();
for (T t : t2) {
if (!list1.contains(t)) {
list2.add(t);
}
}
return list2;
}
compare(new Integer[] { 1, 2, 3 }, new Integer[] {
1, 2, 3, 4 })
public static <T> List<T> compare(T[] t1, T[] t2) {
List<T> list1 = Arrays.asList(t1);
List<T> list2 = new ArrayList<T>();
for (T t : t2) {
if (!list1.contains(t)) {
list2.add(t);
}
}
return list2;
}
compare(new Integer[] { 1, 2, 3 }, new Integer[] {
1, 2, 3, 4 })
本文介绍了一种在Java中比较两个整数数组并找出不同元素的方法。通过将数组转换为列表,并利用循环和条件判断来实现。具体展示了如何编写一个泛型方法来处理这种比较,并给出一个具体的例子。

被折叠的 条评论
为什么被折叠?



