如 数组1:[0,1,2,3,4]
数组2:[0,1,2]
那么怎么找出不同的元素3,4呢??
双循环好像去不出来???
求代码
数组2:[0,1,2]
那么怎么找出不同的元素3,4呢??
双循环好像去不出来???
求代码
解决方案 »
- 菜鸟求救!大侠们帮忙解决一下吧
- httpClinet循环抓网页时出现异常
- 求教二个笔试题,请大家能给出合理的答案
- 点击菜单项,让它弹出另一个窗口,该如何做?
- hashcode
- 如何从一个collection根据collection对象里的变量来排序,返回的是已经排序好的collection
- 在把一应用程序改成applet时出现了这样的问题,大家给看看什么原因
- 为什么import xxx.ClassName;可以,而import xxx.*;不行?
- 哪位高手知道如何使用Servlet实现打印功能----高分回报
- What should I do to close a Applet? Use this.destroy() can't do that??????.
- 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表中剩下的就是不同的。