title: Java get next Enum value or start from first
body: <p>I have an enum in Java as following:</p>
  
  <pre><code>public enum Cars
  {
     Sport,
     SUV,
     Coupe
  }
  </code></pre>
  
  <p>And I need to get the next value of the enum. So, assuming I have a variable called <em>myCurrentCar</em>:</p>
  
  <pre><code>private Cars myCurrentCar = Cars.Sport;
  </code></pre>
  
  <p>I need to create a function that when called set the value of <em>myCurrentCar</em> to the next value in the enum. If the enum does not have anymore values, I should set the variable to the first value of the enum.
  I started my implementation in this way:</p>
  
  <pre><code>public Cars GetNextCar(Cars e)
  {
    switch(e)
    {
       case Sport:
         return SUV;
       case SUV:
         return Coupe;
       case Coupe:
         return Sport;
       default:
         throw new IndexOutOfRangeException();
    }
  }
  </code></pre>
  
  <p>Which is working but it's an <strong>high maitenance</strong> function because every time I modify the enum list I have to refactor the function.
  Is there a way to split the enum into an array of strings, get the next value and transform the string value into the original enum? So in case I am at the end of the array I simply grab the first index</p>