【发布时间】:2020-12-22 04:35:54
【问题描述】:
我无法创建一个将二维整数数组作为输入的方法。输出将是第二个二维数组,在第一个数组的相应行中具有相同数量的元素,这次元素的类型将是布尔值。输出的值将多次对应于数组中存在的值。
这里我提供了一个例子:
如果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: ");
}
public static boolean[][] gridOfMultiples(int[][] inputArray){
boolean [][] gridOfMultiples = new boolean[inputArray.length][];
Arrays.fill(gridOfMultiples, false);
for(int i=0; i<inputArray.length.length; i++){
for (int j=0; j<inputArray.length[i]; j++){
if(inputArray.length[i][j]==inputArray.length[i][j]){
gridOfMultiples[i][j] = true;
break;
}
return gridOfMultiples;
}
}
return whoIsPrime;
}
【问题讨论】:
-
您需要将数组中的每个值与所有其他值进行比较,然后适当地设置布尔数组。然后继续下一个。目前,您只是在进行一对一的比较。
-
我怎样才能做到这一点我也相当新的数组?
-
您的 gridOfMultiples 方法无法编译,因为它存在几个问题。您可能希望将其注释为伪代码,以免混淆其他人。您还将一个值与自身进行比较
inputArray.length[i][j] == inputArray.length[i][j]不幸的是,它既无用(它始终为真)又无效(无法编译)。
标签: java arrays loops for-loop boolean