title: Finding intersection of two sorted arrays in Java
body: <pre><code>    public class intersect {
    public static void find(int[] a, int[] b, int[] acc) 
    {
      int position = 0;
      for (int j = 0; j &lt; a.length; j++) {
        for (int k = 0; k&lt;b.length; k++) {
          if (a[j] == b[k]) {          
            acc[position] = b[k];
            position++;
          }
        }
      }
      System.out.println(java.util.Arrays.toString(acc));
    }
  
  public static void main (String[] s)
  {
    int[] acc = new int[2];
    int[] a = {1,2,3};
    int[] b = {2,3,4};
    find(a, b, acc);
  }
  }
  </code></pre>
  
  <p>I have written the above code to solve the problem.
  But if you see, the function is very limited because I have to change the length of the acc every time. That means I have to know how many elements are  intersecting. In this case, the array {1,2,3} and {2,3,4} have {2,3} in common, so the length of the acc would be 2.</p>
  
  <p>I am sure there are millions of ways of tackling this problem, but I cannot seem to think of a way of fixing this.</p>
  
  <p>Please help!</p>