我的目標是找出數組a和b的交集值並將它們存儲到一個新數組c中,因此打印輸出爲:3,10,4,8。我如何將給定的值賦給第三個數組c?查找兩個數組的交集
public static void main(String[] args) {
int a[] = {3, 10, 4, 2, 8};
int[] b = {10, 4, 12, 3, 23, 1, 8};
int[] c;
int i=0;
for(int f=0;f<a.length;f++){
for(int k=0;k<b.length;k++){
if(a[f]==b[k]){
//here should be a line that stores equal values of 2 arrays(a,b) into array c
}
}
}
for (int x=0; x<c.length; x++){
System.out.println(c[i]);
}
}
}
如果這不是家庭作業要求嚴格陣列ickies,看'Set'接口 - 如果需要維護或維護,它稍微複雜一點,但不是很多。 – 2012-10-16 16:26:48
你的代碼就在那裏,而我(ndex)從0開始並沒有被使用。 – CBredlow
檢查此鏈接http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html –