int[] a = {10, 20, 50, 60, 70}; int[] b = {10, 30, 50}; ArrayList<Integer> lists = new ArrayList<Integer>(); for(int i : a) { for(int j : b) { if(i == j){ break; } lists.add(i); break; } }
给你一个不用collection写的: int[] a = {10,20,30,40,50,70,80,90}; int[] b = {10,20,60,40,30}; int[] c = new int[a.length + b.length]; System.arraycopy(a, 0, c, 0, a.length); System.arraycopy(b, 0, c, a.length, b.length);
int count = 0; for (int i = 0; i < c.length; i++) { for (int j = i + 1; j < c.length; j++) { if (c[i] == c[j]) { c[i] = c[j] = 0; count++; break; } } } int[] d = new int[c.length - (count+1)]; count= 0; for (int i = 0; i < c.length; i++) { if (c[i] != 0) d[count++] = c[i]; }
for (int i = 0; i < d.length; i++) { System.out.println(d[i]); }
public class CollectionTest { public static void main(String[] args) { int[] a = {10,20,30,40,50}; int[] b = {10,20,60}; Set setA = new TreeSet(); Set setB = new TreeSet(); for (int i = 0; i< a.length; i++) { setA.add(new Integer(a[i])); } for (int i = 0; i< b.length; i++) { setB.add(new Integer(b[i])); } Set a_substract_b = new TreeSet(setA); a_substract_b.removeAll(setB); Set b_substract_a = new TreeSet(setB); b_substract_a.removeAll(setA); Set setC = new TreeSet(a_substract_b); setC.addAll(b_substract_a); System.out.println(setC.toString()); } }
2.逐一比较a或b数组,是否与另一数组有重复元素,若无,加入新数组,若有,继续拿下一元素进行比较
3.排序新数组
x=0;
for(i=0; i<sizeof(a); i++)
for(j=0; j<sizeof(b); j++)
{
if(a[i]=b[j])
{
c[x]=a[i];
x++;
}
}最简单最没效率的方法sizeof算数组大小 JAVA里没有
int[] b={20,30,60};
int[] result;
int index=0;
List list=new ArrayList();
for(int i=0;i<a.length;i++)
for(int j=0;j<b.length;j++){
if(a[i]==b[j]){
list.add(index,a[i]);
index++;
}
}
result=new int[list.size()];
for(int i=0; i<list.size();i++){
result[i]=Integer.parseInt(list.get(i).toString());
}
2.将a和b的第一个相比较,将小的那个放入c中,再取a和b中刚才比较小的那组的第2个.相同的继续在该组中取,一直去到最后2个数组中一个放如C结束.
呵呵,在c的数据结果上学到的
int[] a={10,20,30,40,50,60};
int[] b={20,30,60};
int[] result;
int index=0;List tempList = new ArrayList();
for (int i=0;i<a.length;i++){
tempList.add(a[i]);
}for (int j=0;j<b.length;j++){
if (tempList.contain(b[j]))
tempList.remove(b[j]);
tempList.add(b[j]);
} result=new int[tempList.size()];
for(int i=0; i<tempList.size();i++){
result[i]=Integer.parseInt(tempList.get(i).toString());
}
不过还要加个排序
seanla(夜黑心死)的看着挺好,但是我试了下,出现了异常
if (tempList.contain(b[j]))
tempList.remove(b[j]);//在这里抛出了
java.lang.IndexOutOfBoundsException: Index: 10, Size: 5
tempList.add(b[j]);
int[] a={10,20,30,40,50};
int[] b={10,20,60};
int[] result;
ArrayList temp=new ArrayList();
for(int i=0;i<a.length;i++){
temp.add(a[i]);
}
for(int j=0;j<b.length;j++){
Integer bb=new Integer(b[j]);
if(temp.contains(bb)){
temp.remove(bb);
continue; //关键
}
temp.add(bb);
}
result =new int[temp.size()];
for(int m=0;m<temp.size();m++){
result[m]=Integer.parseInt(temp.get(m).toString());
System.out.println(result[m]);
}
结果为
30
40
50
60
如果不加continue的话就是:
30
40
50
10
20
60
先remove了,再循环发现没有又将10,20加进来了
int[] b = { 10, 20, 60 };
Set set = new TreeSet();
int aLength = a.length;
int bLength = b.length;
for (int i = 0; i < aLength; i++) {
set.add(new Integer(a[i]));
}
for (int j = 0; j < bLength; j++) {
if (set.contains(new Integer(b[j]))) {
set.remove(new Integer(b[j]));
} else {
set.add(new Integer(b[j]));
}
}
Integer[] result = (Integer[]) set.toArray(new Integer[0]);
System.out.println(result.length);
public static void main(String[] arg) {
int a[]={10,20,30,40,50};
int b[]={10,20,60};
int total=0, c[]=new int[a.length+b.length];
for(int i=0; i<a.length; i++)
if(isDif(a[i],b)) c[total++]=a[i];
for(int i=0; i<b.length; i++)
if(isDif(b[i],a)) c[total++]=b[i];
for(int i=0; i<total; i++) System.out.print(c[i]+" ");
}
static boolean isDif(int x, int[] y) {
boolean ok=true;
for(int i=0; i<y.length; i++)
if(x==y[i]) ok=false;
return ok;
}
}
int[] b = {10, 30, 50};
ArrayList<Integer> lists = new ArrayList<Integer>();
for(int i : a) {
for(int j : b) {
if(i == j){
break;
}
lists.add(i);
break;
}
}
int[] a = {10,20,30,40,50,70,80,90};
int[] b = {10,20,60,40,30};
int[] c = new int[a.length + b.length]; System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
int count = 0;
for (int i = 0; i < c.length; i++) {
for (int j = i + 1; j < c.length; j++) {
if (c[i] == c[j]) {
c[i] = c[j] = 0;
count++;
break;
}
}
} int[] d = new int[c.length - (count+1)];
count= 0;
for (int i = 0; i < c.length; i++) {
if (c[i] != 0) d[count++] = c[i];
}
for (int i = 0; i < d.length; i++) {
System.out.println(d[i]);
}
int[] a = {10,20,30,40,50};
int[] b = {10,20,60}; Set setA = new TreeSet();
Set setB = new TreeSet(); for (int i = 0; i< a.length; i++) {
setA.add(new Integer(a[i]));
}
for (int i = 0; i< b.length; i++) {
setB.add(new Integer(b[i]));
} Set a_substract_b = new TreeSet(setA);
a_substract_b.removeAll(setB); Set b_substract_a = new TreeSet(setB);
b_substract_a.removeAll(setA); Set setC = new TreeSet(a_substract_b);
setC.addAll(b_substract_a); System.out.println(setC.toString()); }
}
int[] b={3,5,6,3,7};
ArrayList c=new ArrayList();
Arrays.sort(a);
Arrays.sort(b);
int index=0;
for(int i=0;i<a.length;i++)
{
if((index=Arrays.binarySearch(b,a[i]))<0)
c.add(a[i]);
}
for(int i=0;i<b.length;i++)
{
if((index=Arrays.binarySearch(a,b[i]))<0)
c.add(b[i]);
}
Object[] d=c.toArray();
for(int i=0;i<d.length;i++)
{
System.out.println(d[i]);
}
int[] a = { 10, 20, 30, 40, 50, 60 };
int[] b = { 20, 30, 60 };
int[] result; List tempList = new ArrayList();
for (int i = 0; i < a.length; i++) {
tempList.add(new Integer(a[i]));
} for (int j = 0; j < b.length; j++) {
if (tempList.contains(b[j]))
tempList.remove(new Integer(b[j]));
else
tempList.add(new Integer(b[j]));
} result = new int[tempList.size()];
for (int i = 0; i < tempList.size(); i++) {
result[i] = Integer.parseInt(tempList.get(i).toString());
System.out.println(result[i]);
} }