Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算数组中的整数出现次数_Java_Arrays_Find Occurrences - Fatal编程技术网

Java 计算数组中的整数出现次数

Java 计算数组中的整数出现次数,java,arrays,find-occurrences,Java,Arrays,Find Occurrences,我试图计算数组中整数的出现次数。我可以通过拼凑一些我在网上找到的代码来让它工作,但我真的不明白为什么它能工作。我得到的是: int[] hand = {2, 4, 3, 2, 4}; int[] numOccurence = new int[hand.length]; for (int i = 0; i < hand.length; i++) numOccurence[hand[i]]++; for (int i = 1; i < numOccu

我试图计算数组中整数的出现次数。我可以通过拼凑一些我在网上找到的代码来让它工作,但我真的不明白为什么它能工作。我得到的是:

int[] hand = {2, 4, 3, 2, 4};
int[] numOccurence = new int[hand.length];


    for (int i = 0; i < hand.length; i++)
        numOccurence[hand[i]]++;

    for (int i = 1; i < numOccurence.length; i++)
        if (numOccurence[i] > 0)
            System.out.println("The number " + i + " occurs " + numOccurence[i] + " times.");
输出为: 数字2出现2次。 数字3出现1次。 数字4出现2次


此代码如何正确计算出现的次数?我不知道它是如何做到这一点的。提前谢谢你

实际上,为图片创建直方图的方法是相同的

您将创建一个表,在其中收集引用。 NumoCurence[0]将存储0的编号 NumoCurence[1]将存储1的数量 等等

这就是我们要做的

for (int i = 0; i < hand.length; i++)
    numOccurence[hand[i]]++;
这是相同的,但写起来更快

numOccurence[2]++; 

这只是因为你运气好才起作用。尝试将hand数组中的第二个元素设为5,然后看看会发生什么。这是因为当前指针索引中的数字被用作数组numOccurence的索引。如果数字大于或等于NumCurence的长度,您将获得ArrayIndexOutOfBoundsException

因此,您可以更好地使用映射,其中键是数字,值可以是其计数

大概是这样的:-

Map<Integer, Integer> numOccurence = new HashMap<Integer, Integer>();
for (int i = 0; i < hand.length; i++) {
    int cnt = 1;
    if (numOccurence.containsKey(hand[i])) {
        cnt = numOccurence.get(hand[i]);
        cnt++;
    }
    numOccurence.put(hand[i], cnt);
}

这段代码实际上不起作用。至少对作者的用例是这样,但对你的用例可能不是这样

尝试使用{2,4,99,2,4};就像手一样,它会失败

作者将手边找到的数字作为数组numOccurence的索引。
numOccurence具有以下结构:{nb occ为0;nb occs为1;…;nb occs为4}。这里99将超出范围。

当您创建数组时

int[] numOccurence = new int[hand.length];
它由它们的默认值填充。对于基元int,该值为0


当然,只有当hand包含的数字小于或等于数组的最大索引长度-1时,这才有效,否则它就是ArrayIndexOutOfBound,先生

这种执行计数的方法称为

计数排序的优点是它的速度快。缺点是排序大数字时需要内存

代码中有一个bug:

int[] numOccurence = new int[hand.length];

NumoCurence的长度必须与列表中的最高数字相同,而不是与列表中的数字相同。尝试将其中一个数字更改为15,您将得到一个异常。

首先,代码是错误的。您应该将NumoCurence数组的大小设置为手动数组+1的最大数值。例如:

int[] hand = {2, 100};
int[] numOccurence[] = new int[101];
显然,您应该通过编程找到最大数量

现在让我们来看看算法。 它从hand数组中获取每个数字,将其视为NumoCurence索引值,并在hand数组中将该索引处的数字递增1。请注意,NumoCurence数组的所有元素在开始时默认为0

int[] hand = {2, 4, 3, 2, 4};
int[] numOccurence = new int[5];
步骤:

i=0没有任何事情发生,因为手数组中没有0

i=1与0的情况相同

i=2在hand数组中有两个2的数字,所以我们对numcurence[2]+=1进行了两次运算,结果是0+1+1=2。所以我们得到了numocurence[2]=2


对于从0到hand数组中的max number的所有数字,它在此处继续:100。

代码在给定的hand数组中迭代。它将遇到的每个值作为索引放入数组numOccurrence。对于手头上的每个数字n,这种情况的发生频率与手头上的n的发生频率相同,并且每次发生这种情况时,numOccurrence的第n个元素都会增加

因此,numOccurrence实际上是一个计数器数组,假设数组元素初始化为0

这种方法的缺点:

分配的计数器数量取决于handarray中的数字大小。 如果手动数组中的数字分布稀疏,则绝大多数分配的空间永远不会被使用。 可供替代的
您可以通过首先对指针进行排序来改进代码。在排序数组中,给定数字的所有引用的索引都是连续的,因此您只需要一个计数器就可以对排序数组进行一次扫描来编译频率。

您是否获得了正确的输出,并使用更大的数字,例如{100、200、300、400}?在询问之前,您是否尝试过调试以查看它在做什么?很清楚它是如何计算的。我不知道你们为什么要测试这个程序。用户只需要对输出进行解释。我不知道你们为什么要测试这个程序。用户只需要对输出进行解释。我不知道你们为什么要测试这个程序。用户只需要对输出进行解释。
int[] hand = {2, 4, 3, 2, 4};
int[] numOccurence = new int[5];