在按列排序的2d数组中查找2d数组元素的索引 [英] Finding indexes of 2d array elements in a sorted 2d array by columns

查看:66
本文介绍了在按列排序的2d数组中查找2d数组元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个具有相同维数的整数的2D数组.我将通过一个示例来说明我需要什么,其中Sjm1是原始"链接.数组和Sjm2数组具有与Sjm1相同的值,但是每列的值以递增顺序排序(即"0"是列0中Sjm1的最小值),因此 Sjm2 [0] [0] = 0;然后"70"是列0⇒ Sjm2 [1] [0] = 70 等中Sjm1的下一个最小值.我有一种方法可以对"Sjm1"进行排序数组为"Sjm2".

I have two 2D arrays of integers of the same dimension that I need to compare. I am going to explain what I need with an example where Sjm1 is the "original" array and the Sjm2 array has the same values as Sjm1 but the values of each column are ordered in increasing order (i.e. "0" is the min value of Sjm1 in column 0 so Sjm2[0][0]=0; then "70" is the next min value of Sjm1 in column 0 ⇒ Sjm2[1][0]=70; and so on). I have a method to sort a "Sjm1" array to "Sjm2".

现在,我需要构建一个输出"数组(整数的2D数组),其中每列中的值表示Sjm1数组中与Sjm2中的列元素一致的行号.例如, Output [0] [0] = 5 是因为 Sjm1 [0] [0] = 366 Sjm2 [5] [0] = 366 ; Output [1] [0] = 2 是因为 Sjm1 [1] [0] = 104 Sjm2 [2] [0] = 104 ;等等.

Now I need to build an "output" array (2D array of integers) where the values in each column represent the number of the row in Sjm1 array that coincides with the elements of the column in Sjm2. For example, Output[0][0]=5 because Sjm1[0][0]=366 is Sjm2[5][0]=366; Output[1][0]=2 because Sjm1[1][0]=104 is Sjm2[2][0]=104; and so on).

因此,对于前面显示的示例,所需的输出必须为以下内容:

Thus, for the previously presented example, the needed output must be the following:

我试图在Java中构建一个方法,在该方法中我传递了两个2D整数数组,但无法按需工作:

I tried to build a method in Java where I pass the two 2D arrays of integers but is not working as needed:

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int k;
    int[][] output = new int[Sjm1.length][Sjm1[0].length];
    for (int j = 0; j < Sjm1.length; j++) {
        k = 0;
        for (int m = 0; m < Sjm1[0].length; m++) {
            if (Sorted_Sjm2[j][m] == Sjm1[j][m]) {
                output[j][m] = k;
                k++;
            }
        }
    }
    return output;
}

输出显然不是我所需要的:

The output is clearly not what I need:

[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]

如果有人可以帮助我,我会很高兴.

I'll be glad if someone can help me.

推荐答案

我认为您没有正确更新 k 的值,如果我了解您的需求,一旦找到所需的值,寻找只是将 k 的值更新为在其中找到该值的索引.请注意,如果您有重复的值,它将只使用找到的第一个.

I think you are not updating the value of k correctly, if I understood what you need, once you find the value you are looking for just update the value of k to the index you found the value in. Note that if you have repeated values it will only take the first it found.

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int k;
    int[][] output = new int[Sjm1.length][Sjm1[0].length];
    for (int j = 0; j < Sjm1.length; j++) {
        k = 0;
        for (int m = 0; m < Sjm1[0].length; m++) {
            if (Sorted_Sjm2[j][m] == Sjm1[j][m]) {
                k = j;
                output[j][m] = k;
                break;
            }
        }
    }
    return output;
}

这篇关于在按列排序的2d数组中查找2d数组元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆