两个集合遍历查找不重复

时间:2014-06-07 10:37:47   收藏:0   阅读:150

int[] tab1 = new int[]{2009,2010,2013};
int[] tab2 = new int[]{2009,2010,2009,2010,2014};

//这里首先遍历表2
for(int i=0; i<tab2.length; ++i) {
//取出表2中的数据
int elem2 = tab2[i];

boolean find = false;

//这里遍历表1
for(int j=0;j<tab1.length; ++j) {

//取出表1中的数据
int elem1 = tab1[j];

if(elem2 == elem1) {

//表2中的elem2在与表1中的elem1相等
//说明elem2在表1中,将find设置为true,然后跳出循环,找表1中的下一个数据
find = true;

System.out.println("表2中数据 " + elem2 + " 在表1中,其数组下标为:" + i);
break;
}
}

//在表1循环结束之后,如果find仍然为false的话,说明表2中的elem2
//在表1中没有被找到,即不存在
if(!find) {
System.out.println("表2中数据 " + elem2 + "在表1中不存在!");
}

两个集合遍历查找不重复,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!