Counting pairs of elements in a circular ArrayList containing -1, 0, and 1 I have the following method:

public void test() {

    List&lt;Integer&gt; slopeChanges = new ArrayList&lt;Integer&gt;();
    slopeChanges.add(0);
    slopeChanges.add(0);
    slopeChanges.add(1);
    slopeChanges.add(0);
    slopeChanges.add(1);

    List&lt;Double&gt; sixProbabilites = new ArrayList&lt;Double&gt;();

    int count_neg10 = 0, count_neg11 = 0, count_10 = 0, count_1neg1 = 0, count_0neg1 = 0, count_01 = 0;

    for(int i=0; i&lt;slopeChanges.size(); i++) {
        if(i+1 != slopeChanges.size()) {
            if((slopeChanges.get(i) == -1) &amp;&amp; (slopeChanges.get(i+1) == 0)) {count_neg10++;}
            else if((slopeChanges.get(i) == -1) &amp;&amp; (slopeChanges.get(i+1) == 1)) {count_neg11++;}
            else if((slopeChanges.get(i) == 1) &amp;&amp; (slopeChanges.get(i+1) == 0)) {count_10++;}
            else if((slopeChanges.get(i) == 1) &amp;&amp; (slopeChanges.get(i+1) == -1)) {count_1neg1++;}
            else if((slopeChanges.get(i) == 0) &amp;&amp; (slopeChanges.get(i+1) == -1)) {count_0neg1++;}
            else if((slopeChanges.get(i) == 0) &amp;&amp; (slopeChanges.get(i+1) == 1)) {count_01++;}
        }else {
            if((slopeChanges.get(i) == -1) &amp;&amp; (slopeChanges.get(0) == 0)) {count_neg10++;}
            else if((slopeChanges.get(i) == -1) &amp;&amp; (slopeChanges.get(0) == 1)) {count_neg11++;}
            else if((slopeChanges.get(i) == 1) &amp;&amp; (slopeChanges.get(0) == 0)) {count_10++;}
            else if((slopeChanges.get(i) == 1) &amp;&amp; (slopeChanges.get(0) == -1)) {count_1neg1++;}
            else if((slopeChanges.get(i) == 0) &amp;&amp; (slopeChanges.get(0) == -1)) {count_0neg1++;}
            else if((slopeChanges.get(i) == 0) &amp;&amp; (slopeChanges.get(0) == 1)) {count_01++;}
        }
    }

    System.out.println(slopeChanges);

    if(count_neg10 != 0) {System.out.println("count_neg10 = " + count_neg10);}
    if(count_neg11 != 0) {System.out.println("count_neg11 = " + count_neg11);}
    if(count_10 != 0)    {System.out.println("count_10 = " + count_10);}
    if(count_1neg1 != 0) {System.out.println("count_1neg1 = " + count_1neg1);}
    if(count_0neg1 != 0) {System.out.println("count_0neg1 = " + count_0neg1);}
    if(count_01 != 0)    {System.out.println("count_01 = " + count_01);}

    if(count_neg10 != 0) {sixProbabilites.add((double)count_neg10 / ((double)slopeChanges.size()));}
    if(count_neg11 != 0) {sixProbabilites.add((double)count_neg11 / ((double)slopeChanges.size()));}
    if(count_10 != 0) {sixProbabilites.add((double)count_10 / ((double)slopeChanges.size()));}
    if(count_1neg1 != 0) {sixProbabilites.add((double)count_1neg1 / ((double)slopeChanges.size()));}
    if(count_0neg1 != 0) {sixProbabilites.add((double)count_0neg1 / ((double)slopeChanges.size()));}
    if(count_01 != 0) {sixProbabilites.add((double)count_01 / ((double)slopeChanges.size()));}
}


The slopeChanges list contains symbols that are either 0, 1, or -1. I would like to count frequencies of sub-blocks that are either 01, 10, -11, 1-1, -10, 0-1. The method that I showed does what I want, for example, this block of symbols 00101 should result in count_10 = 2 which means 10 sub-block occurs twice while 01 (i.e., count_01 = 2) occurs twice. Please note that the we consider the first symbol is also last.

As you can see, there are lot of duplication in the code in order to perform what I want. Is there a better way to re-write this function in a very elegant style?
The 2nd way is to use the array with unique index calculated from current and next value:

public void test() {

    List&lt;Integer&gt; slopeChanges = new ArrayList&lt;Integer&gt;();
    slopeChanges.add(0);
    slopeChanges.add(0);
    slopeChanges.add(1);
    slopeChanges.add(0);
    slopeChanges.add(1);

    /*
    static final int count_neg10 = 2; // -1 + 3 * (0 + 1);
    static final int count_neg11 = 5; // -1  + 3 * (1 + 1);
    static final int count_10 = 4; // 1 + 3 * (0 + 1);
    static final int count_1neg1 = 1; // 1 + 3 * (-1 + 1);
    static final int count_0neg1 = 0; // 0  + 3 * (-1 + 1);
    static final int count_01 = 6; // 0  + 3 * (1 + 1);*/

    int[] count = {0, 0, 0, 0, 0, 0, 0};
    String[] count_names = {"count_0neg1", "count_1neg1", "count_neg10", "", "count_10", "count_neg11", "count_01"};

    List&lt;Double&gt; sixProbabilites = new ArrayList&lt;Double&gt;();

    for(int i = 0, j = slopeChanges.size() -1; i&lt;slopeChanges.size(); i++) {
      if (slopeChanges.get(i) != slopeChanges.get(j)) {
        count[slopeChanges.get(i) + 3 * (slopeChanges.get(j) + 1) ]++;
      } 
      j = i;
    }

    System.out.println(slopeChanges);

    for(int i = 0; i&lt;7; i++) {
      if(count[i] != 0) {
        System.out.println(count_names[i] + " = " + count[i]);
        sixProbabilites.add((double)count[i] / ((double)slopeChanges.size()));
      }
    }
}