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

这意味着它数一二,二三,三四,只有一五。

我不确定如何编写输出 2 的代码。这里需要二进制搜索吗?

有人能解释一下吗?

4

10 回答 10

5

如果您期望数组值介于 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]);
于 2013-10-31T11:51:52.960 回答
4

我建议您使用Map

  • 如果其中不存在数字,则将其添加1。
  • 如果数字存在,则将其值加 1。

然后将地图打印为 akeyvalue

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

地图是否包含密钥2?不,将其与值 1 相加。(与和相同)3地图 是否包含?是的!将其值加 1。现在4 的值为2 ....45
4

于 2013-10-31T11:41:15.733 回答
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.

于 2013-10-31T11:50:51.780 回答
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 在第 1 个位置有 0 的数量,在第 2 位置有 1 的数量,依此类推

于 2013-10-31T11:51:29.910 回答
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));
}
于 2013-10-31T11:45:19.813 回答
2

使用 Map 存储计数值:

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
于 2013-10-31T11:57:58.313 回答
1

一种方法是使用地图。当您读取每个数字上的第一个数组时,检查它是否存在于映射中,如果存在则仅增加分配给数字(键)的值,如果不存在则在映射中创建一个值为“1”的新键。

查看http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

于 2013-10-31T11:43:08.880 回答
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
于 2013-10-31T12:05:42.130 回答
1

我更喜欢这样的通用解决方案:

public static <T> Map<T, Integer> toCountMap(List<T> itemsToCount) {
    Map<T, Integer> countMap = new HashMap<>();

    for (T item : itemsToCount) {
        countMap.putIfAbsent(item, 0);
        countMap.put(item, countMap.get(item) + 1);
    }

    return countMap;
}
于 2019-07-01T19:57:06.890 回答
0
//Count the times of numbers present in an array
private HashMap<Integer, Integer> countNumbersInArray(int[] array) {
    HashMap<Integer, Integer> hashMap = new HashMap<>();
    for (int item : array) {
        if (hashMap.containsKey(item)) {
            hashMap.put(item, hashMap.get(item) + 1);
        } else {
            hashMap.put(item, 1);
        }
    }
    return hashMap;
}
于 2021-09-24T15:12:09.053 回答