3

嗨,我正在尝试通过一个二维数组(特别是 4x4 数组)并且都找到任何重复的数字,然后计算该数字重复的次数。到目前为止,我有 4 个 for 循环可以工作,但是比我真正想要的要多。

int counter1 =1;
    String huh="";

    for (int x = 0; x< dataTable.length; x++)
    {
        for (int y=0; y< dataTable.length; y++)
        {
            for (int z = 0; z< dataTable.length; z++)
            {
                for (int a=0; a< dataTable.length; a++)
                {
                    if ( x != z && x !=a && y != z && y !=a)
                    {
                        if (dataTable[x][y] == dataTable[z][a])
                        {
                        counter1++;
                        }
                    }   
                }
            }
        if (counter1 > 1)
        {
        huh += ("\n " + dataTable[x][y] + " repeats " + counter1 + " times!");
        }
        counter1=1;
        }
    }

基本上,这在它将我的数组中的每个数字与包括自身在内的所有其他数字进行比较的意义上是有效的(但 if 语句阻止它自己计算)。基本上我需要输出来说明一些简单的事情,比如

The number 3 repeats 3 times

但是,按照我的设置的工作方式,每次比较数组中每个位置的数字 3 时,它都会将相同的语句添加到字符串中。那么我的方法是否正确,只需要一些调整?还是完全错了,我需要完全不同的东西?我只是在我大学的初学者编程课上,所以我们只知道 Java 的基础知识,比如数组、循环和其他一些东西。

4

4 回答 4

2

I think the best approach would be to maintain a Map<Integer, Integer> that keeps track of the number frequencies (i.e. it maps each number in the array to the number of times it appears). It would not be hard to loop through the entire array and update this map accordingly. What you're doing now seems way more complicated than it really needs to be (in my opinion).

And why are you using 4 for-loops? Perhaps I'm misunderstanding the purpose of your specific code, but you should only need two to loop over a 2D-array (and to ultimately count the number-frequencies):

for (int[] a : array)
    for (int i : a)
        // do something

Relevant documentation:

于 2012-11-14T00:34:08.743 回答
2

只需将此数组转换为 aMap<Integer, Integer>然后打印出来,如下所示:

    public static void main(String[] args) throws Exception {
        final int[][] dataTable = new int[][] {
                new int[] {0, 1, 2, 1},
                new int[] {0, 1, 3, 1},
                new int[] {0, 1, 2, 2},
                new int[] {0, 1, 2, 0}
        };

        final Map<Integer, Integer> map = new HashMap<Integer, Integer> ();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                final int value = dataTable[i][j];
                final Integer currentCount = map.get(value);
                final Integer newCount;
                if (currentCount == null) {
                    newCount = 1;
                }
                else {
                    newCount = currentCount + 1;
                }

                map.put (value, newCount);
            }
        }

        for (final Map.Entry<Integer, Integer> entry : map.entrySet()) {
            System.out.println(String.format ("The number %d repeats %d times", entry.getKey(), entry.getValue()));
        }
    }   

在这里您可以找到结果。

于 2012-11-14T00:38:00.650 回答
0

你可以有一个 n*n 行和 2 列的数组:

/*being n the number of rows/columns*/
int count[]][] = new int[n*n][2];

for (int i = 0; i < dataTable.length; i++) {

    for (int k = 0; k < dataTable.length; k++) {

        /*this loop finds the position in which it should go*/
        for (int h = 0; h < n*n; h++) {
            if (count[h][0] == dataTable[i][k]) {
                break;
            }

            /*Assuming that '0' is not a possible number in dataTable, use '-1' or a number that */
            if (count[h][0] == 0) {
                break;
            }
        }

        count[h][0] = dataTable[i][k];
        count[h][1]++;
    }
}
于 2012-11-14T00:50:20.200 回答
0

正如其他人所建议的那样,最通用的解决方案是使用地图。但是,如果数组值在相对较小的范围内,则可以使用数组而不是映射。如果min是(至多)数组中的最小值并且max(至少)是最大值:

public int[] getFrequencyMap(int[][] array, int min, int max) {
    int[] map = new int[max - min + 1];
    for (int[] row : array) {
        for (int val : row) {
            map[val - min]++;
        }
    }
    return map;
}

在返回的数组中,索引处的值val - min将是该值val在数组中出现的次数。

于 2012-11-14T00:39:27.303 回答