使用 IntStream 检查 2D 数组中的数组

我正在检查一组坐标()是否存在于一组坐标()中。我在其他文章中看到过如何连接2D数组,以便可以在孤独中搜索它,但我不确定如何将其转换为我的问题。感谢您的帮助!coorcoorArrayIntStreamint


示例数组:


int[][] coorArray = {{1,2},{2,2},{3,0}};

int[] coor = {1,2};


潇潇雨雨
浏览 77回答 2
2回答

一只萌萌小番薯

可以使用流().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

Cats萌萌

这是另一个没有 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中是否有其他可用的东西,但这应该满足要求。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java