Let's assume the items
array consists of the following items {3.1, 3.1, 3.1, 3.2, 3.2, 3.3, 3.4, 3.4, 3.4, 3.4, 3.1, 3.1}
What I want is to count the occurrence of each item in the successive items such that:
3.1 = 3
3.2 = 2
3.3 = 1
3.4 = 4
3.1 = 2
I wrote the following function:
private void displayItems(List<Double> items) {
double current_item=0;
for(int i=0; i<items.size(); i++) {
int count=1;
current_item = items.get(i);
if(i != items.size()) {
for(int j=i+1; j<items.size(); j++) {
double next_item = items.get(j);
if(current_item == next_item) {
count++;
}else {
break;
}
}
System.out.println("item value is " + current_item + " and count is " + count);
}
}
}
I got the following result:
item value is 3.1 and count is 3
item value is 3.1 and count is 2
item value is 3.1 and count is 1
item value is 3.2 and count is 2
item value is 3.2 and count is 1
item value is 3.3 and count is 1
item value is 3.4 and count is 4
item value is 3.4 and count is 3
item value is 3.4 and count is 2
item value is 3.4 and count is 1
item value is 3.1 and count is 2
item value is 3.1 and count is 1
What can I do to show the results like the following:
item value is 3.1 and count is 3
item value is 3.2 and count is 2
item value is 3.3 and count is 1
item value is 3.4 and count is 4
item value is 3.1 and count is 2
Please not that I don't want to count the occurrence of each item in the entire array, I just want to count its occurrence in the successive items only.