title: Arraylist(even/odd) swapping elements java
body: <p>Hello folks,
               I am just trying to swap the list items irrespective of the odd or even list. I sense there might be another way of doing swap when the list size is odd. i am just patching the code based on the errors i see on console. Is there any better way to swap the list items when the list is odd not with if condition inside the else. </p>
  
  <pre><code>package org.ada.crm;
  
  import java.util.ArrayList;
  import java.util.Collections;
  
  public class AList {
  
      public static void main(String[]args){
          String[] num = {"four", "score", "and", "seven", "years", "ago","sure"};
          ArrayList&lt;String&gt;list = new ArrayList&lt;String&gt;();
          for(int i=0;i&lt;num.length;i++){
              list.add(num[i]);
          }
          System.out.println("****hhh"+list);
          swapPair(list);
      }
  
      private static void swapPair(ArrayList&lt;String&gt; list) {
  
          int listsize = list.size();
          for(int i=0;i&lt;listsize;i+=2){
          if(listsize%2==0){
              Collections.swap(list, i, i+1);
          }else{
              if(i+1&lt;listsize) ///here i want to know is there any better solution
                  Collections.swap(list, i, i+1);
          }}
          System.out.println("jfsfjdsl"+ list);
      }
  
  }
  </code></pre>