title: Finding largest gap between consecutive numbers in array Java
body: <p>I'm currently working on a homework assignment and the final task of the assignment is to write a method to find the largest gap between consecutive numbers in an unsorted array. Example: if the array had the values {1,2,3,4,5,20} the gap would be 15. Currently the array is holding 20 values generated at random.</p>
  <p>I'm totally lost for how I would make this happen. Initially my idea for how to solve this would be using a for loop which runs through each value of the array with another loop inside to check if the current value is equal to the previous value plus 1. If it is then store that number as the minimum in the range. Another problem I ran into was that I have no idea how to store a second number without overwriting both numbers in the range. Basically nothing i've tried is working and could really use some help or at least a nudge in the right direction.</p>
  <p>What the method does right now is only store the value for &quot;a&quot; after it finds a number that isn't consecutive in the array.</p>
  <p>Here's the code I have so far</p>
  <pre><code>import java.util.Arrays;
  
  class Main {
    public static void main(String[] args) {
      Main m = new Main();
      m.runCode();
    }
  
    public void runCode()
    {
      Calculator calc = new Calculator();
  
      calc.makeList(20);
  
      System.out.println(&quot;List:&quot;);
      calc.showList();
  
      System.out.println(&quot;Max is: &quot; + calc.max());
      System.out.println(&quot;Min is: &quot; + calc.min());
      System.out.println(&quot;Sum is: &quot; + calc.sum());
    System.out.println(&quot;Ave is: &quot; + calc.average());
    System.out.println(&quot;There are &quot; + calc.fiftyLess() + &quot; values in the list that are less than 50&quot;);
    System.out.println(&quot;Even numbers: &quot; + calc.Even());
    }
  }
  
  class Calculator {
  
    int list[] = new int[20];
  
    public void makeList(int listSize)
    {
      for (int count = 0; count &lt; list.length; count++) {
        list[count] = (int) (Math.random() * 100);
      }
    }
  
    public void showList()
    {
      for (int count = 0; count &lt; list.length; count++) 
      {
        System.out.print(list[count] + &quot; &quot;);
      }
    }
  
  public int max()
  {
    int max = list[0];
    for (int count=0; count&lt;list.length; count++){
       if (list[count] &gt; max) {
          max = list[count];
       }
    }
    return max;
  }
  
  public int min()
  {
    int min = list[0];
    for (int count=0; count&lt;list.length; count++){
       if (list[count] &lt; min) {
         min = list[count];
       }
    }
    return min;
   }
  
   public int sum()
   {
     int sum = 0;
     for (int count=0; count&lt;list.length; count++){
       sum = sum + list[count];
     }
     return sum;
   }
  
   public double average()
   {
     int sum = sum();
     double average = sum / list.length;
     return average;
   }
  
   public int fiftyLess()
   {
     int lessThan = 0;
     for (int count =0; count&lt;list.length;count++)
     {
       if (list[count] &lt; 50)
       {
         lessThan++;
       }
  
     }
  
     return lessThan;
  
      
  
   }
  
   public int Even()
   {
     int isEven = 0;
  
     for (int count = 0; count&lt;list.length;count++)
     {
       if (list[count] % 2 == 0)
       {
         isEven++;
  
       }
     }
  
     return isEven;
   }
  
   public int Gap()
   {
  
    int a = 0;
    int b = 0;
    int gap = math.abs(a - b);
     for (int count = 1; count&lt;list.length;count++)
     {
       if (list[count] != list[count] + 1)
       {
        a =list[count];
  
  
       }
     }
   }
  }
  </code></pre>