计算数组中的出现次数 (Java) [英] Counting an Occurrence in an Array (Java)

查看:27
本文介绍了计算数组中的出现次数 (Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全被难住了.我休息了几个小时,我似乎无法弄清楚这一点.好烦啊!

I am completely stumped. I took a break for a few hours and I can't seem to figure this one out. It's upsetting!

我知道我需要检查数组中的当前元素,看看它是否出现在数组的其他地方.这个想法是输出以下内容:

I know that I need to check the current element in the array and see if it appears elsewhere in the array. The idea is to output the following:

要求用户输入 10 个整数,并将这些整数分配给一个数组(因此数字"作为该方法的参数).假设我输入了1, 1, 2, 3, 3, 4, 5, 6, 7, 8".打印出来的结果应该是1出现2次.2出现1次.3出现2次.4出现1次.5出现1次.6出现1次.7出现1次.8出现1次."此打印将在单独的方法中完成.

The user is asked to enter 10 integers and those integers are assigned to an array (hence the "numbers" as a paremeter for the method). Let's say I enter "1, 1, 2, 3, 3, 4, 5, 6, 7, 8." The printed results should be "1 occurs 2 times. 2 occurs 1 time. 3 occurs 2 times. 4 occurs 1 time. 5 occurs 1 time. 6 occurs 1 time. 7 occurs 1 time. 8 occurs 1 time." This printing will be done in a separate method.

除了我创建的用于计算出现次数的方法外,我的代码中的所有内容都可以正常工作.

Everything in my code works except for this method that I created to count the occurrences.

public static int getOccurrences(int[] numbers)
{
    int count = 0;

    for (int i = 0; i < numbers.length; i++)
    {
        int currentInt = numbers[i];;

        if (currentInt == numbers[i])
        {
            count++;
        }
    }

    return count;
}

我知道问题出在哪里.我将数组中的当前整数元素设置为变量 currentInt.if 语句计算数组中的每个整数元素,因此输出为[I@2503dbd3 出现 10 次".

I know what the issue is here. I set the current integer element in the array to the variable currentInt. The if statement counts every integer element in the array, hence the output being "[I@2503dbd3 occurs 10 times."

如何跟踪数组中每个元素的出现次数?

How do I keep track of the occurrences of each element in the array?

推荐答案

package countoccurenceofnumbers;

import java.util.Scanner;
public class CountOccurenceOfNumbers {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int [] num = new int[100]; 
        int [] count = new int[100];
        //Declare counter variable i
        //and temp variable that will
        //temporarily hold the value
        //at a certain index of num[] array
        int i,temp = 0;
        System.out.println("Enter the integers between 1 and 100: ");

        //Initialize num[] array with user input
        for(i=0; i < num.length; i++){
            num[i] = input.nextInt();
            //expected input will end when user enters zero
            if(num[i] == 0){
                break;
            }
        }//end of for loop

        //value at a given index of num array 
        //will be stored in temp variable
        //temp variable will act as an index value
        //for count array and keep track of number
        //of occurences of each number
        for(i = 0; i < num.length; i++){
                temp = num[i];
                count[temp]++;
            }//end of for looop

        for(i=1; i < count.length; i++){

            if(count[i] > 0 && count[i] == 1){
             System.out.printf("%d occurs %d time\n",i, count[i]);
             }
            else if(count[i] >=2){
                System.out.printf("%d occurs %d times\n",i, count[i]);
            }


         }//end of for loop

    }//end of main
    }//end of CountOccurrenceOfNumbers

///////////输出////////////////////

///////////OUTPUT//////////////////////

输入 1 到 100 之间的整数:
2 5 6 5 4 3 23 43 2 0
2 出现 2 次
3 出现 1 次
4 出现 1 次
5出现 2 次
6 出现 1 次
23 出现 1 次
43发生 1 次
BUILD SUCCESSFUL(总时间:3 分 23 秒)

Enter the integers between 1 and 100:
2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
BUILD SUCCESSFUL (total time: 3 minutes 23 seconds)

这篇关于计算数组中的出现次数 (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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