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中是否有其他可用的东西,但这应该满足要求。
添加回答
举报