将一个二维数组中搜索数字,然后输出一个布尔数组。

huangapple go评论73阅读模式
英文:

Taking a 2d array and searching for a number then outputting a boolean array

问题

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; i++) {
            for (int j = 0; j < inputArray[i].length; j++) {
                if (inputArray[i][j] == inputArray[i][j]) {
                    gridOfMultiples[i][j] = true;
                    break;
                }
            }
        }
        return gridOfMultiples;
    }
}
英文:

Im having trouble creating a method that takes as input a two-dimensional integer array. The output will be a second two-dimensional array with equal number of elements in the corresponding rows of the first array, this time the type of the elements will be booleans. The values of the output will correspond to the value existing in the array multiple times.

Here I have provided an example:

If inputArray is the output of the method would be the following boolean array: [[ 4, 9], [ 9, 5, 3]]. Since 9 is in the input array twice, both positions in the output that correspond to those two instances are true, all of the other values only occur once, so their values are false like this [[ false, true ], [ true, false, false]].

The code is provided below.

class SearchAndPrint{
public static void main(String[] args){
int[][] testCase = 
{{4, 9, 10, 9},
{5, 4, 7, 10, 11},
{4, 2}};
System.out.println(&quot;The test case array: &quot;);
for (int i=0; i&lt;testCase.length; i++){
for (int j=0; j&lt;testCase[i].length; j++){ 
System.out.print(testCase[i][j]+&quot; &quot;);
}
System.out.println();      
}        
boolean[][] founds = gridOfMultiples(testCase);
System.out.println(&quot;Positions with repeated numbers are marked below: &quot;);
}
public static boolean[][] gridOfMultiples(int[][] inputArray){
boolean [][] gridOfMultiples = new boolean[inputArray.length][];
Arrays.fill(gridOfMultiples, false);
for(int i=0; i&lt;inputArray.length.length; i++){
for (int j=0; j&lt;inputArray.length[i]; j++){
if(inputArray.length[i][j]==inputArray.length[i][j]){
gridOfMultiples[i][j] = true;
break;
}
return gridOfMultiples;
}
}
return whoIsPrime;
}

答案1

得分: 1

以下是针对单个一维数组的处理方法。数组进行两次迭代,数组的每个后续值与其他所有值进行比较。请注意,已知大小的原始布尔数组会自动初始化为全false值。

int[] vals = {1, 2, 3, 2, 4, 5, 6, 4, 9, 4};
boolean[] bools = new boolean[vals.length];
for (int i = 0; i < vals.length - 1; i++) {
    for (int k = i + 1; k < vals.length; k++) {
        if (vals[i] == vals[k]) {
            // 确保两者都为true,但继续检查
            // 因为可能还有更多重复的值。
            bools[i] = true; 
            bools[k] = true;
        }
    }
}
System.out.println(Arrays.toString(bools));

输出

[false, true, false, true, true, false, false, true, false, true]

只有2和4出现了多次。

英文:

Here is how you would do it for a single D array. Array is iterated twice and each subsequent value of the array is compared to all the others. Note that primitive boolean arrays of known size are automatically initialized to all false values.

int [] vals = {1,2,3,2,4,5,6,4,9,4};
boolean[] bools = new boolean[vals.length];
for (int i = 0; i &lt; vals.length-1; i++) {
for (int k = i+1; k &lt; vals.length; k++) {
if (vals[i] == vals[k]) {
// ensure both are true but continue checking
// as there may be more duplicate values.
bools[i] = true; 
bools[k] = true;
}
}
}
System.out.println(Arrays.toString(bools));

Prints

[false, true, false, true, true, false, false, true, false, true]

Only 2 and 4 occur multiple times.

huangapple
  • 本文由 发表于 2020年9月3日 08:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63715166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定