如何在 Java 中尋找兩個陣列的交集?
在 Java 中查詢兩個陣列的交集,可以使用兩個迴圈。外部迴圈用於迭代第一個陣列的元素,而第二個迴圈用於迭代第二個陣列的元素。在第二個迴圈中比較兩個陣列的元素
示例
public class IntersectionOfTwoArrays {
public static void main(String args[]) {
int myArray1[] = {23, 36, 96, 78, 55};
int myArray2[] = {78, 45, 19, 73, 55};
System.out.println("Intersection of the two arrays ::");
for(int i = 0; i<myArray1.length; i++ ) {
for(int j = 0; j<myArray2.length; j++) {
if(myArray1[i]==myArray2[j]) {
System.out.println(myArray2[j]);
}
}
}
}
}輸出
Intersection of the two arrays :: 78 55
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP