2 回答

TA貢獻1795條經驗 獲得超7個贊
可以使用流().anyMatch()
來執行此檢查:
int[][] coorArray = {{1,2},{2,2},{3,0}};
int[] coor = {1,2};
boolean exist = Arrays.stream(coorArray).anyMatch(e -> Arrays.equals(e, coor));
System.out.println("exist = " + exist);
輸出:
exist = true
否則,當輸入數組中不存在坐標時:
int[][] coorArray = {{4,2},{2,2},{3,0}};
int[] coor = {1,2};
boolean exist = Arrays.stream(coorArray).anyMatch(e -> Arrays.equals(e, coor));
System.out.println("exist = " + exist);
輸出:
exist = false

TA貢獻1805條經驗 獲得超9個贊
這是另一個沒有 lambda 表達式的示例,如果你喜歡;)。由每個坐標的簡單和每個坐標的檢查組成。
public static boolean exists(int[][] coords, int[] coord){
for(int[] c : coords){
if(c[0] == coord[0] && c[1] == coord[1]) {
return true;
}
}
return false;
}
我不確定API中是否有其他可用的東西,但這應該滿足要求。
添加回答
舉報