计算数组中的次数(Java)

我完全难过了。 我rest了几个小时,我似乎无法想出这个。 这让人心烦意乱!

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

要求用户输入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次。” 此打印将以单独的方法完成。

我的代码中的所有内容都有效,除了我创建的用于计算出现次数的方法。

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次”。

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

 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 

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

输入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次
建立成功(总时间:3分23秒)

你需要两个循环:

  1. 在你开始的地方

  2. 一个嵌套循环,作为你当前所在位置前面的一个索引,除非你在最后。

有没有一个你不希望在你的arrays中的数字? 如果是这样,请使用该值(例如-1)作为标记值,​​以便在计算时覆盖您的出现次数。 然后,当您再次浏览数组以查找出现的下一个数字时,如果它具有您的sentinel值,则跳过它。

@NYB你几乎是对的,但你必须输出计数值,并在每个元素检查时从零开始。

  int count=0,currentInt=0; for (int i = 0; i < numbers.length; i++) { currentInt = numbers[i]; count=0; for (int j = 0; j < numbers.length; j++) { if (currentInt == numbers[j]) { count++; } } System.out.println(count); } 

@loikkk我稍微调整了你的代码,以便为每个元素打印出来。

 int[] a = { 1, 9, 8, 8, 7, 6, 5, 4, 3, 3, 2, 1 }; Arrays.sort(a); int nbOccurences = 1; for (int i = 0, length = a.length; i < length; i++) { if (i < length - 1) { if (a[i] == a[i + 1]) { nbOccurences++; } } else { System.out.println(a[i] + " occurs " + nbOccurences + " time(s)"); //end of array } if (i < length - 1 && a[i] != a[i + 1]) { System.out.println(a[i] + " occurs " + nbOccurences + " time(s)"); //moving to new element in array nbOccurences = 1; } } 

你可以在这里找到你的问题的答案

我在我的例子中使用了Arrays.sort()方法:

 public class MyTest { /** * @param args */ public static void main(String[] args) { int[] a = {1, 9, 8, 8, 7, 6, 5, 4, 3, 3, 2, 1}; Arrays.sort(a); int nbOccurences = 0; for (int i = 0, length = a.length - 1; i < length; i++) { if (a[i] == a[i + 1]) { nbOccurences++; } } System.out.println("Number same occurences : " + nbOccurences); } } 

您需要对数组中的数字顺序进行排序。 您可以使用'sort()'方法,该方法可以将您的数字从最小到最大组织起来。

你还需要两个循环,一个用于比较另一个。 或者在我的解决方案中,我使用’ while语句 ‘然后使用’ for循环 ‘。

如果我解决问题的方法是您正在寻找的方法,我不知道。 也许有更短和/或更好的方法来解决这个问题。 这就是我想到的方式。 祝你好运!

 public static int getOccurrences(int[] numbers){ Array.sort (numbers); //sorts your array in order (i,e; 2, 9, 4, 8... becomes, 2, 4, 8, 9) int count = 0; int start = 0; int move = 0; while(start < numbers.length){ for (int j = 0; j < numbers.length; j++){ int currentInt = numbers[start];; if (currentInt == numbers[j]) { count++; move++; } } if(count == 1){ return ("Number : " + numbers[start] + " occurs " + count + " time "); } else { return ("Number : " + numbers[start] + " occurs " + count + " times "); } count = 0; start = start + move; move = 0; } } 

只需复制并执行它,它将为您提供数组中出现的整数。

 public class noOfOccurence{ public static void main(String[] args){ int a[] = {1,9,4,5,6,7,5,6,7,3,2,5,7,9,0,4,3,5,1,4,6,0,2,3,1,4,3,8}; HashSet al = new HashSet(); //Store the array in set as set will store unique elemnets for(int i=0;i