title: Java Indexing array help. CodingBat
body: <p>The question I am trying to solve is: <a href="http://codingbat.com/prob/p128270" rel="nofollow">http://codingbat.com/prob/p128270</a></p>
  
  <p>Given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array. </p>
  
  <pre><code>front11({1, 2, 3}, {7, 9, 8}) → {1, 7}
  front11({1}, {2}) → {1, 2}
  front11({1, 7}, {}) → {1}
  </code></pre>
  
  <p>My Code so far:</p>
  
  <pre><code>public int[] front11(int[] a, int[] b) {
      int answerindexs = 2;
  
      if(a.length == 0 || b.length == 0)
          answerindexs = 1;
  
      if(a.length == 0 &amp;&amp; b.length == 0)
          answerindexs = 0;
  
      int[] answer = new int[answerindexs];
  
      for (int x = 0; x &lt;= 1; x++){
          if(a.length &gt; 0 &amp;&amp; x == 0)
              answer[0] = a[0];
          else if(b.length &gt; 0 &amp;&amp; x == 1)
              answer[1] = b[0];
      }
      return answer;
  
  
  }
  </code></pre>
  
  <p>This question I have been trying to do by myself is fully stressing me out because every attempt I try to do java doesn't work the way I thought it did. 
  The only test I did not pass is;</p>
  
  <pre><code>front11({}, {2, 8}) → {2}
  </code></pre>
  
  <p>Because I get an index out of bound error, and I am having trouble trying to solve for this specific test. As I am not sure how to check if my answer array has an element already in it as answer.length is always 2 eventhough it has no assigned element in each index as it is defaulted to zero.</p>
  
  <p>Any help appreciated, also if anyone could improve my two if statements at the beginning (It works for little numbers but I know when it gets to larger numbers I can't write code like this). I wanted to answer this question using ArrayList as I can just .add(), but this question specifies an array which is annoying to find out how many slots to preset.</p>