int[] a ={2,3,6,8,9,11};
int[] b={1,4,6,8,11};
两个从小到大排序好的数组,要求只用一次循环就找出相同的元素。(注意前提条件是排序好的数组),思路就是两两比较,小的往前移动。
public static void main(String[] args) {
int[] a ={2,3,6,8,9,11};
int[] b={1,4,6,8,11};
int x = 0;
int i = 0;
for(; i<a.length;){
if(x>b.length-1){
break;
}
if(a[i] < b[x]){
++i;
continue;
}
if(a[i] > b[x]){
++x;
continue;
}
if(a[i] == b[x]){
System.out.println(a[i]);
++i;
++x;
}
}
}