title: Find the last occurrence of an array Java?
body: <p>I'm new to Java. I would like to get the index of the last occurrence in an array using loop. However, I don't understand why I can't.</p>
  
  <p>This is the array:
      {2, 3, 4, 5, 4, 5, 3}</p>
  
  <p>I would like to get the index of the last 4 in it.</p>
  
  <p>My code is:</p>
  
  <pre><code>public static void main(String args[]){
      int[] nums = {2, 3, 4, 5, 4, 5, 3};
  
      int pos4 = 0;
      for (int k = nums.length -1; k &gt;= 0; k--){
          if (nums[k] == 4){
              pos4 = k;
              break;
          }
      System.out.print(pos4);
      }
  }
  </code></pre>
  
  <p>The result is: 00 ??</p>
  
  <p>When I change to: </p>
  
  <pre><code>public static void main(String args[]){
      int[] nums = {2, 3, 4, 5, 4, 5, 3};
  
      int pos4 = 0;
      for (int k = nums.length -1; k &gt;= 0; k--){
          if (nums[k] == 4){
              break;
          }
      System.out.print(k);
      }
  }
  </code></pre>
  
  <p>I got 65 ???</p>
  
  <p>However, when I print directly from the loop I get the index correctly:</p>
  
  <pre><code>public static void main(String args[]){
      int[] nums = {2, 3, 4, 5, 4, 5, 3};
  
      int pos4 = 0;
      for (int k = nums.length -1; k &gt;= 0; k--){
          if (nums[k] == 4){
              System.out.print(k);
              break;
          }
      }
  }
  </code></pre>
  
  <p>Can anyone tell me why? Thanks a lot!</p>