Let's assume we have an array that contains the following elements:
-1, 0, 0, 1, 0, -1, 0, -1, 1
I would like to count the combinations of consecutive elements such that
-1,0 & 0,0 & 0,1 & 1,0 & 0,-1 & -1,0 & 0,-1 & -1&1
In this case:
The count of -1,0 = 2
The count of 0,1 = 1
The count of 1,0 = 1
and so on
I implemented the following:
List<Double> sixProbabilites = new ArrayList<Double>();
int count_neg10 = 0, count_neg11 = 0, count_10 = 0, count_1neg1 = 0, count_0neg1 = 0, count_01 = 0;
for(int i=0; i<slopeChanges.size(); i++) {
if(i+1 != slopeChanges.size()) {
if((slopeChanges.get(i) == -1) && (slopeChanges.get(i) == 0)) {count_neg10++;}
else if((slopeChanges.get(i) == -1) && (slopeChanges.get(i+1) == 1)) {count_neg11++;}
else if((slopeChanges.get(i) == 1) && (slopeChanges.get(i+1) == 0)) {count_10++;}
else if((slopeChanges.get(i) == 1) && (slopeChanges.get(i+1) == -1)) {count_1neg1++;}
else if((slopeChanges.get(i) == 0) && (slopeChanges.get(i+1) == -1)) {count_0neg1++;}
else if((slopeChanges.get(i) == 0) && (slopeChanges.get(i+1) == 1)) {count_01++;}
}
}
There is no problem with the code but I am just wondering if there is a way to enhance the code especially the if statement. Is there any way to reduce the code size to few lines?