title: (Java) Search indexes of values of an array in another array
body: <p>I have written the following code that takes two arrays and searches the index of the first occurrence of each value from the first array in the second one. For example if <code>first = {15, 10, 18, 17, 15}</code> and <code>second = {10, 15, 10, 17}</code> then the output would be an array with an equal length to <code>first</code> which contains the indices <code>output = {1, 0, -1, 3, 1}</code>, as e.g. 15 occurs in index 1 of the <code>second</code> array, 10 occurs at the 0th index, etc. The index will be -1 if the value in <code>first</code> doesn't occur in <code>second</code>. The code I've written to loop through the arrays is as follows:</p>
  
  <pre><code>public static int[] searchIndexes(int[] first, int[] second) {
  
      int[] indices = new int[first.length];
      int index = -1;
  
      for (int i = 0; i &lt; first.length; i ++) {
          for (int j = 0; j &lt; second.length; j ++) {
              if (first[i] == second[j])
                  index = j;
          }
          indices[i] = index;
      }
      return indices;
  }
  </code></pre>
  
  <p>However, for the given example the output is instead <code>{1, 2, 2, 3, 1}</code>. I think I do understand the issue; since 10 occurs twice in <code>second</code> then the index of the <strong>second</strong> occurrence is recorded, but I don't know how to get around this. Putting a <code>break;</code> statement after the <code>if</code> clause doesn't seem to fix it.</p>