搜索多维数组中的特定行

时间:2022-12-14 21:39:22

I'm new to java programming and I can't wrap my head around one final question in one of my assignments.

我是java编程的新手,我不能在我的一个任务中绕过最后一个问题。

We were told to create a static method that would search a 2-D array and compare the numbers of the 2-D array to an input number...so like this:

我们被告知创建一个静态方法,它将搜索二维数组并将二维数组的数字与输入数字进行比较......所以像这样:

private static int[] searchArray(int[][] num, int N){

private static int [] searchArray(int [] [] num,int N){

Now, the part what we're returning is a new one-dimensional array telling the index of the first number in each row that is bigger than the parameter variable N. If no number is bigger than N, then a -1 is returned for that position of the array.

现在,我们返回的部分是一个新的一维数组,告诉每行中第一个数字的索引大于参数变量N.如果没有数字大于N,则返回-1阵列的那个位置。

So for example a multi-dimensional array named "A":

例如,名为“A”的多维数组:

4 5 6

4 5 6

8 3 1

8 3 1

7 8 9

7 8 9

2 0 4

2 0 4

If we used this method and did searchArray(A, 5) the answer would be "{2,0,0,-1)"

如果我们使用这个方法并且做了searchArray(A,5),答案就是“{2,0,0,-1)”

2 个解决方案

#1


1  

Here is a very good explanation about Java 2D arrays

这是关于Java 2D数组的非常好的解释

    int num[][] = {{4,5,6},{8,3,1},{7,8,9}};
    int N = 5;
    int result[] = new int[num.length];
    for(int i=0; i<num.length; i++){
        result[i] = -1;
        for(int j=0; j<num[0].length; j++){
            if( N < num[i][j] ){
                result[i] = j;
                break;
            }
        }
    }

    for(int i=0; i<result.length; i++){
        System.out.println(result[i]);
    }

The first for loop(The one with a for inside it) traverses the 2D array from top to bottom in a left to right direction. This is, first it goes with the 4 then 5,6,8,3,1,7,8,9.

第一个for循环(内部带有for的循环)以从左到右的方向从顶部到底部遍历2D阵列。这是,首先它与4然后5,6,8,3,1,7,8,9。

First the result array is created. The length depends of the number of rows of num. result[i] is set to -1 in case there are no numbers bigger than N. if a number bigger than N is found the column index is saved result[i] = j and a break is used to exit the for loop since we just want to find the index of the first number greater than N.

首先创建结果数组。长度取决于num的行数。如果没有大于N的数字,result [i]设置为-1。如果找到大于N的数字,则保存列索引result [i] = j并且因为我们使用了break来退出for循环只想找到大于N的第一个数字的索引。

The last for loop just prints the result.

最后一个for循环只打印结果。

#2


0  

Generally when using multi-dimensional arrays you are going to use a nested for loop:

通常,在使用多维数组时,您将使用嵌套的for循环:

for(int i = 0; i < outerArray.length; i++){
   //this loop searches through each row
   for(int j = 0; j < innerArrays.length; j++) {
     //this loop searches through each column in a given row
     //do your logic code here
   }
}

I won't give you more than the basic structure, as you need to understand the question; you'll be encountering such structures a lot in the future, but this should get you started.

我不会给你更多的基本结构,因为你需要理解这个问题;你将来会遇到很多这样的结构,但这应该让你开始。

#1


1  

Here is a very good explanation about Java 2D arrays

这是关于Java 2D数组的非常好的解释

    int num[][] = {{4,5,6},{8,3,1},{7,8,9}};
    int N = 5;
    int result[] = new int[num.length];
    for(int i=0; i<num.length; i++){
        result[i] = -1;
        for(int j=0; j<num[0].length; j++){
            if( N < num[i][j] ){
                result[i] = j;
                break;
            }
        }
    }

    for(int i=0; i<result.length; i++){
        System.out.println(result[i]);
    }

The first for loop(The one with a for inside it) traverses the 2D array from top to bottom in a left to right direction. This is, first it goes with the 4 then 5,6,8,3,1,7,8,9.

第一个for循环(内部带有for的循环)以从左到右的方向从顶部到底部遍历2D阵列。这是,首先它与4然后5,6,8,3,1,7,8,9。

First the result array is created. The length depends of the number of rows of num. result[i] is set to -1 in case there are no numbers bigger than N. if a number bigger than N is found the column index is saved result[i] = j and a break is used to exit the for loop since we just want to find the index of the first number greater than N.

首先创建结果数组。长度取决于num的行数。如果没有大于N的数字,result [i]设置为-1。如果找到大于N的数字,则保存列索引result [i] = j并且因为我们使用了break来退出for循环只想找到大于N的第一个数字的索引。

The last for loop just prints the result.

最后一个for循环只打印结果。

#2


0  

Generally when using multi-dimensional arrays you are going to use a nested for loop:

通常,在使用多维数组时,您将使用嵌套的for循环:

for(int i = 0; i < outerArray.length; i++){
   //this loop searches through each row
   for(int j = 0; j < innerArrays.length; j++) {
     //this loop searches through each column in a given row
     //do your logic code here
   }
}

I won't give you more than the basic structure, as you need to understand the question; you'll be encountering such structures a lot in the future, but this should get you started.

我不会给你更多的基本结构,因为你需要理解这个问题;你将来会遇到很多这样的结构,但这应该让你开始。