title: Filling stalls within an Array ( Big Java Ex 7.22)
body: <p>For Some background here is the problem statement.</p>
  <blockquote>
  <p>It is a well-researched fact that men in a restroom generally prefer
  to maximize their distance from already occupied stalls, by occupying
  the middle of the longest sequence of unoccupied places. For example,
  consider the situation where ten stalls are empty.</p>
  <p>_ _ _ _ _ _ _ _ _ _ The first visitor will occupy a middle position:</p>
  <p>_ _ _ _ _ X _ _ _ _ The next visitor will be in the middle of the empty area at the left.</p>
  <p>_ _ X _ _ X _ _ _ _</p>
  <p>Write a program that reads the number of stalls and then prints out diagrams in the format given above when the stalls become filled, one at a time. Hint: Use an array of boolean values toindicate whether a stall is occupied.</p>
  </blockquote>
  <p>I have also found the solution for which I understand for the most part but am having trouble understanding a bit. Here it is</p>
  <pre><code>import java.util.Scanner;
  class StallLogic
  {
      public void printStalls(boolean [] b)
      {
          for(boolean s:b)
          {
              System.out.print((s?&quot;X&quot; : &quot;_&quot;) + &quot; &quot;);
          }
          System.out.print(&quot;\n&quot;);
      }
      
      public boolean giveFlag(boolean [] b)
      {
          for(boolean s : b)
          {
              if(!s) return false;
          }
          return true;
      }
      
      public int getLongest(boolean [] b)
      {
          int length = 0, temp = 0;
          int len = b.length;
          for(int i = 0; i &lt; len ; i++)
          {
              if (b[i] == false)
              {
                  temp++;
              }
              else{
                  temp = 0;
              }
              if (length &lt; temp)
                  length = temp;
          }
          return length;
      }
      
      public int checkIndex(boolean [] b)
      {
          int length = 0 , temp = 0, ind = 0;
          int len = b.length;
          for (int i = 0 ; i &lt; len ; i++)
          {
              if(b[i] == false)
              {
                  temp++;
              }
              else{
                  temp = 0;
              }
              if (length &lt; temp)
              {
                  ind = i -length;
                  length = temp;
              }
          }
          return ind;
      }
      
      public void findStalls(boolean [] b)
      {
          int loc = checkIndex(b);
          int len = getLongest(b);
          int ind = loc + len / 2;
          b[ind] = true;
      }
  }
  
  public class checkStall
  {
      public static void main(String [] args)
      {
      StallLogic stallin = new StallLogic();  
      System.out.print(&quot;Enter number of stalls&quot;);
      Scanner in = new Scanner(System.in);
      int i = in.nextInt();
      boolean [] stalls = new boolean [i];
      while(!stallin.giveFlag(stalls))    
      {
          stallin.findStalls(stalls);
          stallin.printStalls(stalls);
      }
      
      }
  
      
  }
  </code></pre>
  <p><strong>My Struggles</strong></p>
  <p>I'm having trouble understand what the point of <code>checkIndex</code> really is. I understand that <code>getLongest</code>, will tell us which parts of the arraylist have the most successive <code>_</code>'s</p>
  <p>For example in <code>_ _ _ _ _ X _ _ _ _</code> We check that the left side has more <code>_</code> with <code>getLongest</code>. Now the problem is we need an actual index to put the <code>X</code> in. How Will our program know whether to put it on the left side or the right side of every pre-existing X? This is where I PRESUME the <code>checkIndex</code> comes in.</p>
  <p>Particularly What I am confused about is why I am taking the maximum count of how many _'s there are and adding it with the specific index? I.e <code>int ind = loc + len / 2;</code></p>
  <p>What is <code>checkIndex</code> and <code>int ind = loc + len / 2,</code> exactly doing here? Is there some algorithm for this I am not realizing?</p>