2013-10-31 95 views
0

我已经写存储一些值一个java PROG:爪哇 - 在阵列计数数字

public class array05 { 
    public static void main(String[] args) { 

     //placement of value 
     int arryNum[] = {2,3,4,5,4,4,3}; 

     //placement of index, to start at 0 
     for(int counter=0;counter<arryNum.length;counter++){ 
      System.out.println(counter + ":" + arryNum[counter]); 
     } 
    } 
} 

其中产生这样的输出:
0:2
1:3
2:4
3:5
4:4
5:4
6:3

,现在我需要在此输出#1中计数数字。 输出#2应该是这样的:

1:0
2:1
3:2
4:3
5:1

这意味着它计数ONE 2,TWO 3,三4,并且只有一个5.

我不知道如何编写输出2的代码。 是否需要二分法搜索?

有人可以开灯吗?

+1

问题的陈述是否包括在阵列中的数字有限制吗?也就是说,他们都保证少于10? –

+0

http://stackoverflow.com/q/8098601/1007273 – hovanessyan

+0

你试图实现计数排序。 –

回答

2

,如果你在你的阵列值1-5之间期待(我假设这从您的预期输出数)

int arryNum[] = { 2, 3, 4, 5, 4, 4, 3 }; 
int[] counter = new int[] { 0, 0, 0, 0, 0 }; 
for (int i = 0; i < arryNum.length; i++) { 
    counter[arryNum[i] - 1]++; 
} 

for (int i = 0; i < counter.length; i++) 
    System.out.println((i + 1) + ":" + counter[i]); 
+0

感谢您的帮助,我可以通过这种方法轻松理解!但是我不明白这个部分[arryNum [i] - 1],你介意解释吗? – Ree

+0

这很容易理解,但如果数组包含的值不在1到5之间,那么它就会中断。例如,对于这个数组:“{0,2,3,4,5,4,4,3}”。由于这些数字似乎并不代表只能在1-5之间的任何数字,所以这不是一种好的做法。 (可能是确定的作业)的阵列 –

+0

@rzrz指数为0,而你的号码从1 – user902383

4

我建议你使用一个Map

  • 如果号码不存在它,用1
  • 如果该号码存在添加,添加1〜它的价值。

然后打印地图作为keyvalue

例如,对于您的阵列{2,3,4,5,4,4,3}这将工作如下:

是否在地图中包含的关键2?否,请将其与值1相加。(3,45相同)
地图是否包含4?是!给它的值加1。现在关键 4具有值的2
...

+2

他会说“但我们还没有学过地图!” –

+0

没时间像现在学习地图!远非有用忽视,并且是解决这个问题的最简单方法! – NickJ

+0

FYI:如果实现假定所有映射值(不是键)不能为空,而不是检查,看是否存在的关键,只是试图获得与键的值。如果它返回null,那么你知道该键也不存在。这比通过键循环更快(当你说“containsKey”时会发生什么)。它是O(1)与O(n)。 – MadConan

2

事情是这样的:

//numbers to count 
int arryNum[] = {2,3,4,5,4,4,3}; 

//map to store results in 
Map<Integer, Integer> counts = new HashMap<Integer, Integer>(); 

//Do the counting 
for (int i : arryNum) { 
    if (counts.containsKey(i) { 
     counts.put(i, counts.get(i)+1); 
    } else { 
     counts.put(i, 1); 
    } 
} 

//Output the results 
for (int i : counts.keySet()) { 
    System.out.println(i+":"+counts.get(i)); 
} 
+0

没有人发现故意的错误 - 我有hasKey()代替的containsKey()。现在更正。 :) – NickJ

3

这是一个解决这个问题:

import java.util.Arrays; 

public class array05 { 
    public static void main(String[] args) { 
     //placement of value 
     int arryNum[] = {2,3,4,5,4,4,3}; 

     // Sort the array so counting same objects is easy 
     Arrays.sort(arryNum); 

     int index = 0; // The current index 
     int curnum;  // The current number 
     int count;  // The count of this number 
     while (index < arryNum.length) { 
      // Obtain the current number 
      curnum = arryNum[index]; 

      // Reset the counter 
      count = 0; 

      // "while the index is smaller than the amount of items 
      // and the current number is equal to the number in the current index, 
      // increase the index position and the counter by 1" 
      for (; index < arryNum.length && curnum == arryNum[index]; index ++, count++); 

      // count should contain the appropriate amount of the current 
      // number now 
      System.out.println(curnum + ":" + count); 
     } 
    } 
} 

人们使用Map贴好解决方案,所以我想我会提供一个始终有效的良好解决方案(不只是针对当前值),而不使用Map

+0

嗨,感谢您的帮助,但如果我没有在阵列中的一个,如何显示它呢? EG - 1:0 – Ree

+0

您的声明:“这意味着它计数了一个2,2个3,3个4,只有一个5”让我想到它只需要计算数组中存在的项目。我很抱歉,如果这个不适合你的需求:) –

+1

这不是O(n)的复杂性,但至少为O(n的log(n)) – user902383

3
If you don't want to use Map, this is how you would do it with Arrays only(if you have numbers from 1 to 9 only) 

Integer[] countArray = new Integer[10] 

// Firstly Initialize all elements of countArray to zero 
// Then 
for(i=0;i<arryNum.length();i++){ 

int j = arryNum[i]; 
countArray[j]++; 

} 

这countArray为0在第一的位置,在第2位中1的个数等

2

使用地图存储计数值:

import java.util.HashMap; 
import java.util.Map; 

class array05{ 
    public static void main(String[] args){ 
     // Container for count values 
     Map <Integer, Integer> result = new HashMap<Integer, Integer>(); 
     int arryNum[] = {2,3,4,5,4,4,3}; 
     for(int i: arryNum){ //foreach more correct in this case 
      if (result.containsKey(i)) result.put(i, result.get(i)+1); 
      else result.put(i, 1); 
     } 
     for (int i: result.keySet()) System.out.println(i + ":" + result.get(i)); 
    } 
} 

结果如下:

2:1 
3:2 
4:3 
5:1 
+0

谢谢你的地图建议,如果我想包含不在数组中的数字,那么怎么样?例如:没有数字1因此输出是1:0 – Ree

+0

它可能,但更复杂。在这种情况下,你不能使用泛型。可能是你需要使用'getClass'(http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html)从'Object'转换 –

+0

查看我的答案更新 –

1

你可以试试这个方式太

int arrayNum[] = {2,3,4,5,4,4,3}; 
    Map<Integer,Integer> valMap=new HashMap<>(); 
    for(int i:arrayNum){ // jdk version should >1.7 
     Integer val=valMap.get(i); 
     if(val==null){ 
      val=0; 
     } 
     valMap.put(i,val+1); 
    } 
    Arrays.sort(arrayNum); 
    for(int i=0;i< arrayNum[arrayNum.length-1];i++){ 
     System.out.println(i+1+" : "+((valMap.get(i+1)==null) ? 0:valMap.get(i+1))); 
    } 

出把

1 : 0 
    2 : 1 
    3 : 2 
    4 : 3 
    5 : 1 

但下列方式是更好的

int arrayNum[] = {2,3,4,5,4,4,3}; 
    Arrays.sort(arrayNum); 
    int countArray[]=new int[arrayNum[arrayNum.length-1]+1]; 
    for(int i:arrayNum){ 
     countArray[i]= countArray[i]+1; 
    } 
    for(int i=1;i<countArray.length;i++){ 
     System.out.println(i+" : "+countArray[i]); 
    } 

出把

1 : 0 
    2 : 1 
    3 : 2 
    4 : 3 
    5 : 1