如何查找,并在2维数组算重复? [英] How do i find and count duplicates in a 2 dimensional array?

查看:131
本文介绍了如何查找,并在2维数组算重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我试图经过一个二维阵列(特别是一个4×4阵列),并都发现重复任何数字,然后计算的次数的数目重复的数目。到目前为止,我有4个为循环工作的但是做的比我真正想要的更多。

Hi I am trying to go through a two-dimensional array (specifically a 4x4 array) and both find any numbers that repeat, then count the number of times that the number repeats. So far I have 4 for loops that work however do more than what i really want.

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语句保持它从本身计数)。基本上我需要的输出陈述一些简单的像

Basically this works in the sense that it compares every number in my array with every other number including itself (but the if statement keeps it from counting itself). Basically i need the output to state something simple like

The number 3 repeats 3 times

但与我的设置工作,将添加到相同的语句,每次它比较它的每一个在我的阵位的数字3串的方式。那么,我的方法在所有正确的,只需要一些调整?或者是完全错误的,我需要完全不同的东西?我只是在我的大学初学编程类,所以我们只知道Java基础迄今如数组,循环和一些其他的东西。

However with the way that my setup works it would add to the string that same statement each time it compared the number 3 in each of its places in my array. So is my method correct at all and only needs some tweaking? or is it wrong altogether and i need something totally different? I am only in a beginner programming class at my college so we only know the basics of java so far like arrays, loops, and a few other things.

推荐答案

我认为最好的办法是保持一个地图&LT;整数,整数GT; ,保持跟踪的数量的频率(即它映射数组它出现的次数中的每一个号码)。它不会通过整个阵列很难循环,并相应地更新该地图。你现在正在做的似乎的办法的比它更复杂真正需要的(在我看来)。

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).

和为什么您使用的 4 的for循环?也许我误解你的具体code的目的,但你应该只需要两个遍历一个二维数组(和最终计数的数频率):

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:

这篇关于如何查找,并在2维数组算重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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