如何比较2个数组中不同的元素? 如 数组1:[0,1,2,3,4]数组2:[0,1,2]那么怎么找出不同的元素3,4呢??双循环好像去不出来???求代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.util.HashMap;import java.util.Map;public class test2 { public static void main(String[] args) { int[] num2 = {0, 1, 2, 3, 4, 5}; int[] num1 = {0, 1, 2, 7, 8, 9}; Map<Integer, Integer> map1 = new HashMap<Integer, Integer>(); Map<Integer, Integer> map2 = new HashMap<Integer, Integer>(); int big = num2.length > num1.length ? num2.length : num1.length; int small = num2.length <= num1.length ? num2.length : num1.length; int[] tmp1 = num2.length > num1.length ? num2 : num1; int[] tmp2 = num2.length <= num1.length ? num2 : num1; for (int i = 0; i < small; i++) { map1.put(tmp2[i], 0); } for (int i = 0; i < big; i++) { map2.put(tmp1[i], 0); if (!map1.containsKey(tmp1[i])) { System.out.println(tmp1[i]); } } for (int i = 0; i < small; i++) { map1.put(tmp2[i], 0); if (!map2.containsKey(tmp2[i])) { System.out.println(tmp2[i]); } } }} public class test{ public static void main(String arg[]){int a[] = {1,2,3,4,5};int b[] = {1,2,5};for(int i=0;i<a.length;i++) { if(i>b.length-1){ System.out.print(a[i]); }else if(a[i]!=b[i]) { System.out.print(a[i]); } }}} 表a,表b循环遍历表a,从b里面删除a[i],判断是否删除成功,若成功,则从a里面删除a[i],否则,不操作。最后a表和b表中剩下的就是不同的。 请教个方法内常量的问题 java网络编程——C/S聊天室 数据库中自动完成计算的字段有疑问?? java对象问题 让java桌面程序在任务栏右下脚 【救急!】诡异问题。。。拉了菜单Canvas就不响应鼠标事件!! 文件流的小问题 哪里有JAVA桌面应用程或管理软件下载??? 传递引用的问题 在web页中用网格显示查询数据 JScrollBar如何实现控制窗口滚动?? java acm类库中graphicprogram没法重绘
import java.util.HashMap;
import java.util.Map;
public class test2 {
public static void main(String[] args) {
int[] num2 = {0, 1, 2, 3, 4, 5};
int[] num1 = {0, 1, 2, 7, 8, 9};
Map<Integer, Integer> map1 = new HashMap<Integer, Integer>();
Map<Integer, Integer> map2 = new HashMap<Integer, Integer>();
int big = num2.length > num1.length ? num2.length : num1.length;
int small = num2.length <= num1.length ? num2.length : num1.length;
int[] tmp1 = num2.length > num1.length ? num2 : num1;
int[] tmp2 = num2.length <= num1.length ? num2 : num1;
for (int i = 0; i < small; i++) {
map1.put(tmp2[i], 0);
}
for (int i = 0; i < big; i++) {
map2.put(tmp1[i], 0);
if (!map1.containsKey(tmp1[i])) {
System.out.println(tmp1[i]);
}
}
for (int i = 0; i < small; i++) {
map1.put(tmp2[i], 0);
if (!map2.containsKey(tmp2[i])) {
System.out.println(tmp2[i]);
}
}
}
}
public class test{
public static void main(String arg[]){
int a[] = {1,2,3,4,5};
int b[] = {1,2,5};
for(int i=0;i<a.length;i++)
{
if(i>b.length-1){
System.out.print(a[i]);
}else if(a[i]!=b[i])
{
System.out.print(a[i]);
}
}
}
}
循环遍历表a,从b里面删除a[i],判断是否删除成功,若成功,则从a里面删除a[i],否则,不操作。
最后a表和b表中剩下的就是不同的。