Java 在二维数组中查找多个匹配数字并返回布尔数组

Java 在二维数组中查找多个匹配数字并返回布尔数组,java,arrays,search,boolean,2d,Java,Arrays,Search,Boolean,2d,我在创建一个以二维整数数组作为输入的方法时遇到了麻烦。输出将是第二个二维数组,在第一个数组的相应行中具有相同数量的元素,这一次元素的类型将是布尔型。输出值将与数组中存在的值多次对应 该方法应使用searchForMultiple方法 这里我提供了一个例子: 如果inputArray为 该方法的输出将是以下布尔数组:[[4,9],[9,5,3]] 由于9在输入数组中存在两次,因此输出中与这些位置对应的两个位置 两个实例为true,所有其他值只出现一次,因此它们的值为false,如下所示:[[fal

我在创建一个以二维整数数组作为输入的方法时遇到了麻烦。输出将是第二个二维数组,在第一个数组的相应行中具有相同数量的元素,这一次元素的类型将是布尔型。输出值将与数组中存在的值多次对应

该方法应使用
searchForMultiple
方法

这里我提供了一个例子:

如果
inputArray
为 该方法的输出将是以下布尔数组:
[[4,9],[9,5,3]]
由于9在输入数组中存在两次,因此输出中与这些位置对应的两个位置 两个实例为true,所有其他值只出现一次,因此它们的值为false,如下所示:
[[false,true],[true,false,false]]

代码如下所示:

class SearchAndPrint{
    public static void main(String[] args){
        int[][] testCase = 
        {{4, 9, 10, 9},
            {5, 4, 7, 10, 11},
            {4, 2}};
        System.out.println("The test case array: ");
        for (int i=0; i<testCase.length; i++){
            for (int j=0; j<testCase[i].length; j++){ 
                System.out.print(testCase[i][j]+" ");
            }
            System.out.println();      
        }        
        boolean[][] founds = gridOfMultiples(testCase);
        System.out.println("Positions with repeated numbers are marked below: ");
        printGrid(founds);
    }
    public static boolean[][] gridOfMultiples(int[][] testCase){
        boolean [][] gridOfMultiples = new boolean[testCase`.length];
        for(int i = 0; i<testCase.length; i++){
            for(int j =0; j<testCase[i].length; j++){
                if(testCase[i][j]==testCase[i][j]){
                    gridOfMultiples[i][j]= true;
                }
                else
                gridOfMultiples[i][j]= false;
            }
        }
    }
    public static boolean searchForMultiple(int[][] inputArray, int search){
    }
    public static void printGrid(boolean[][] inputGrid){
    }
}
类搜索和打印{
公共静态void main(字符串[]args){
int[][]测试用例=
{{4, 9, 10, 9},
{5, 4, 7, 10, 11},
{4, 2}};
System.out.println(“测试用例数组:”);

对于(int i=0;i来说,您的代码似乎可以像这样简单,不需要使用
searchForMultiple()
方法,这对性能是有害的

publicstaticvoidmain(字符串[]args){
int[]testCase={{4,9,10,9},
{5, 4, 7, 10, 11},
{4, 2} };
System.out.println(Arrays.deepToString(testCase));
布尔[][]founds=gridOfMultiples(testCase);
System.out.println(Arrays.deepToString(founds));
}
公共静态布尔[][]gridOfMultiples(int[][]测试用例){
映射频率=Stream.of(testCase)
.flatMapToInt(IntStream::of)
.boxed()
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting());
boolean[]founds=new boolean[testCase.length][];
for(int i=0;i1;
}
回归基金;
}
输出

[[4,9,10,9],[5,4,7,10,11],[4,2]]
[[真,真,真,真],[假,真,假,真,假],[真,假]]