英文:
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("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;
}
答案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 < vals.length-1; i++) {
for (int k = i+1; k < 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论