# how to print occurence of values in the array from 1 to9?

• 04-30-2013, 11:49 AM
anusha
how to print occurence of values in the array from 1 to9?
public class ArrayExample {
public static void main(String[] args) {
Integer[] array = {1,78,23,5,6,7,8,6,5,8,8,50};

ArrayList l=new ArrayList();
ArrayList<Integer> s=new ArrayList<Integer>();
for(int i=0;i<array.length;i++)
{ int a=array[i];
if(a>9)
{

}
else
}

}

countStringOccurences(s);
System.out.println("this are 2digit number:"+l);
}

private static void countStringOccurences(ArrayList<Integer> strArray) {
HashMap<Integer, Integer> countMap = new HashMap<Integer, Integer>();

for (Integer string : strArray) {

if (!countMap.containsKey(string)) {
countMap.put(string, 1);
} else {
Integer count = countMap.get(string);
count = count + 1;
countMap.put(string, count);
}
}
printCount(countMap);
}

private static void printCount(HashMap<Integer, Integer> countMap) {
Set<Integer> keySet = countMap.keySet();
for (Integer string : keySet) {
System.out.println(string + " : " + countMap.get(string));
}
}
}

o/p is:
1 : 1
5 : 2
6 : 2
7 : 1
8 : 3
this are 2digit number:[78, 23, 50]

but i have to show like
0:1
1:1
2:0
3:0
...
...
9:0
this are 2digit number:[78, 23, 50]